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

使用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>";
?>
  • 写回答

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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)