【问题标题】:user session and displaying user information用户会话和显示用户信息
【发布时间】:2014-08-16 01:48:24
【问题描述】:

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

<?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 中的用户名。” 我不知道下一步该做什么。请推荐!!!

【问题讨论】:

  • 您在设置会话的页面上的任何位置都没有session_start()
  • 将 if 后面的代码包装到 else{} 语句中。问题是你的header已经设置好了,但是你的html中的php也编译好了,没有$_SESSION["username"]

标签: php html css session usersession


【解决方案1】:

当我将代码更改为以下代码时,上述代码有效:

<?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); 


    ?>

谢谢你。

【讨论】:

    【解决方案2】:

    更新您下面显示的部分代码

    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");
    }
    

    当您想使用 Posted 值设置 $_SESSION["username"] 以及 $_SESSION["password"] 时,必须使用 $username$password,因为您正在这些变量中获取 Posted 值。

    【讨论】:

      【解决方案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;
              $_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 语句的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-21
        • 2018-12-15
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 2012-08-29
        相关资源
        最近更新 更多