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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置