douluohan3403
2017-04-26 04:58
浏览 325
已采纳

使用header()重定向后会话变量丢失

I have got index.php file that takes username and password from users, then it redirects to process_login.php that compares these credentials with SQL database to authorize the users. Now if the user is authorized, I want to get all the data about this user and want to use in other PHP files. I am using sessions to do so, but somehow they are not working. I know they are so many similar questions, but none of them worked. Here is my process_login.php code

    <?php
  session_start();
  require_once('connectdatabase.php');

if(isset($_POST) && !empty($_POST)) {
  $username = $_POST['username'];
  $password = $_POST['password'];

  $sql = "SELECT * FROM users WHERE USERNAME='$username' AND PASSWORD='$password'";
  $result = mysqli_query($connection, $sql);
  echo $count = mysqli_num_rows($result);

  if($count == 1) {
    $row = mysqli_fetch_assoc($result);
    $_SESSION['first_name'] = $row["FIRST_NAME"];
    $_SESSION['last_name'] = $row["LAST_NAME"];
    $_SESSION['email'] = $row["EMAIL"];
    $_SESSION['username']=$username;
    header('Location: ../../src/welcome.php');
exit();
  }
  else {
    header('Location: ../../src/index.php');
  }
}
?>

Now I want those variables on welcome.php file.

And this is my welcome.php code

<?php
  session_start();
  $fist_name = $_SESSION['first_name'];
  echo "<script>console.log('$first_name');</script>";
?>

图片转代码服务由CSDN问答提供 功能建议

我有index.php文件,它从用户那里获取用户名和密码,然后重定向到比较的process_login.php 这些凭据用SQL数据库来授权用户。 现在,如果用户已获得授权,我想获取有关此用户的所有数据,并希望在其他PHP文件中使用。 我正在使用会话这样做,但不知何故他们没有工作。 我知道他们有很多类似的问题,但没有一个有效。 这是我的process_login.php代码

 &lt;?php 
 session_start(); 
 require_once('connectdatabase.php'); 
 
if(isset(  $ _POST)&amp;&amp;!empty($ _ POST)){
 $ username = $ _POST ['username']; 
 $ password = $ _POST ['password']; 
 
 $ sql =“SELECT  * FROM users WHERE USERNAME ='$ username'AND PASSWORD ='$ password'“; 
 $ result = mysqli_query($ connection,$ sql); 
 echo $ count = mysqli_num_rows($ result); 
 
 if  ($ count == 1){
 $ row = mysqli_fetch_assoc($ result); 
 $ _SESSION ['first_name'] = $ row [“FIRST_NAME”]; 
 $ _SESSION ['last_name'] = $ row [  “LAST_NAME”]; 
 $ _SESSION ['email'] = $ row [“EMAIL”]; 
 $ _SESSION ['username'] = $ username; 
 header('Location:../../ src  /welcome.php');
exit();
} 
 else {
 header('Location:../../ src / index.php'); 
} 
} 
?
?&gt;  ; 
   
 
 

现在我想在welcome.php文件中使用这些变量。

这是我的welcome.php代码

 &lt;?php 
 session_start(); 
 $ fist_name = $ _SESSION  ['first_name']; 
 echo“&lt; script&gt; console.log('$ first_name');&lt; / script&gt;”; 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongsigan2044 2017-04-26 05:06
    已采纳

    It's because you are using $fist_name rather than $first_name. And edit your echo part

    <?php
      session_start();
      $fist_name = $_SESSION['first_name'];
      echo "<script>console.log('$first_name');</script>";
    ?>
    

    To

    <?php
      session_start();
      $first_name = $_SESSION['first_name'];
      echo $first_name;
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsaxw4201 2017-04-26 05:32

    I wanted to comment but I can't so here is my suggestion for you.

    When something like your issue happens to me I tend to echo the $_SESSION all of them to see if they're actually set or not.

    Below is a small PHP script which does the same but I'm using PDO as the DB API.

    if (isset($_REQUEST["pWord"])){
        $inmPword = md5($_REQUEST["pWord"]);
    
        $loginData = "SELECT * FROM userlogin WHERE pWord = :pWord";
        $loginDataQuery = $dbConnect -> prepare($loginData);
        $loginDataQuery -> bindParam(':pWord', $inmPword);
        $loginDataQuery -> execute();
    
        if ($row = $loginDataQuery -> fetch(PDO::FETCH_ASSOC)){
            //Time to set the session
            $_SESSION["uId"] = $row["uId"];
            $_SESSION["uRole"] = $row["uRole"];
            $_SESSION["fName"] = $row["fName"];
            $_SESSION["lName"] = $row["lName"];
    
            echo "3";
        }else{
            echo "4";
        }
    }
    

    I think it's better not do the row count and echo it. Something like this might help.

     $sql = "SELECT * FROM users WHERE USERNAME='$username' AND PASSWORD='$password'";
      $result = mysqli_query($connection, $sql);
    
      if($row = mysqli_fetch_assoc($result)) {
        $_SESSION['first_name'] = $row["FIRST_NAME"];
        $_SESSION['last_name'] = $row["LAST_NAME"];
        $_SESSION['email'] = $row["EMAIL"];
        $_SESSION['username']=$username;
        header('Location: ../../src/welcome.php');
    exit();
      }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题