简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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