简体   繁体   English

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

[英]Session Variables are lost after redirecting using 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. 我有index.php文件,它从用户那里获取用户名和密码,然后重定向到process_login.php,将这些凭据与SQL数据库进行比较以授权用户。 Now if the user is authorized, I want to get all the data about this user and want to use in other PHP files. 现在,如果用户已获得授权,我想获取有关此用户的所有数据,并希望在其他PHP文件中使用。 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 这是我的process_login.php代码

    <?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. 现在我想在welcome.php文件中使用这些变量。

And this is my welcome.php code 这是我的welcome.php代码

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

It's because you are using $fist_name rather than $first_name . 这是因为你使用$fist_name而不是$first_name And edit your echo part 并编辑你的echo部分

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

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. 当你的问题发生在我身上时,我倾向于$_SESSION所有人的$_SESSION ,看看他们是否真的被设置了。

Below is a small PHP script which does the same but I'm using PDO as the DB API. 下面是一个小的PHP脚本,它做同样的事情,但我使用PDO作为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();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM