3

我正在做一个项目,在其中进入需要用户名和密码的管理页面。一旦用户名和密码被更正或重定向到其他页面,会话就会创建,其中任何一个页面都不正确。我的代码是:

<?php

    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]="username";
        $_SESSION["password"]="passwoed";
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


?>

在管理页面中,我的代码是:

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>
</body>

问题是我直接重定向到主页而无法进入管理页面。会话注册有问题吗?但是,如果我将 php 代码更改为,我在管理页面上:

<?php
    session_start();    
    if(isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

我想显示我通过$_SESSION[]超变量保存的用户名。我收到错误消息Notice: Undefined index: username in C:\xampp\htdocs\admin_page.php.

提前致谢


我按照你说的修改了代码。

但无论我写

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>

或者

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $username; ?></font>       </div>
</body>

我犯了同样的错误。“注意:未定义索引:C:\xampp\htdocs\admin_page.php 中的用户名。” 我不知道下一步该做什么。请推荐!!!

4

3 回答 3

3

当我将代码更改为:

<?php

    session_start();
    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]=$username;//instead of "username"
        $_SESSION["password"]=$password;
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


    ?>

谢谢你。

于 2014-07-03T05:56:01.663 回答
0

第一件事:如果你想为会话设置变量,那么你必须初始化会话:

<?php

    session_start();
    include_once './connection.php'; 
    file_exists("connection.php");

    $username = $_POST['username'];
    $password = $_POST['password'];


    $query=pg_query("select password from users where username like '".$username."' ");

    $mypassword=pg_fetch_object($query)->password;

    if(($password==$mypassword)and($mypassword!=null)){
        $_SESSION["username"]=$username;
        $_SESSION["password"]=$password;
        header("location:admin_page.php");
    }

    else{  
        header("location:attempt.php");
    }


    pg_close($connection); 


    ?>

之后,您也可以编辑 admin_page:

<?php
    session_start();    
    if(!isset($_SESSION["username"])){
        header("location:homepage.php");
    }
else {
?>

<!DOCTYPE html>
<html>
<title>Project</title>
<body>
<div id="loginContainer" align='center'>
    <font color='white'>Logged In As: <?php echo $_SESSION["username"]; ?></font>       </div>
</body>
<?php 
 } 
?>

正如我在评论中所说,如果您没有获得“用户名”变量,则会设置头文件,但页面的其余部分也会“渲染”。这就是我包装到 else 语句的原因。

于 2014-06-26T06:20:46.700 回答
0

更新如下所示的部分代码

if(($password==$mypassword)and($mypassword!=null)){
    $_SESSION["username"]="username";
    $_SESSION["password"]="passwoed";
    header("location:admin_page.php");
}

if(($password==$mypassword)and($mypassword!=null)){
    $_SESSION["username"]= $username;
    $_SESSION["password"]= $password;
    header("location:admin_page.php");
}

正如您要设置$_SESSION["username"]Posted 值以及$_SESSION["password"]必须使用的那样$username,并且$password您正在这些变量中获取 Posted 值。

于 2014-06-25T13:14:21.300 回答