gpt4 book ai didi

php - php session 变量不起作用

转载 作者:行者123 更新时间:2023-12-02 21:28:09 25 4
gpt4 key购买 nike

这是我的登录页面的代码,其中登录脚本检查用户的真实性,然后使用 header 函数重定向到收件箱页面。

<?php
session_start();

include_once('config.php');
$user=htmlentities(stripslashes($_POST['username']));
$password=htmlentities(stripslashes($_POST['password']));
// Some query processing on database

if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){
$_SESSION['loggedIn'] = 'yes';
header("Location:http://xyz/inbox.php?u=$id_user_fetched");
//echo 'Login Successful';
}else{
echo 'Invalid Login';
echo'<br /> <a href="index.html">Click here to try again</a>';
}
}else{
echo mysqli_error("Login Credentials Incorrect!");
}
?>

inbox.php 页面如下所示:

<?php
session_start();
echo 'SESSION ='.$_SESSION['loggedIn'];
if($_SESSION['loggedIn'] != 'yes'){
echo $message = 'you must log in to see this page.';
//header('location:login.php');
}
//REST OF THE CODE

?>

现在使用上面的代码,inbox.php 始终显示输出: SESSION=您必须登录才能查看此页面。这意味着要么未设置 session 变量,要么 inbox.php 无法检索 session 变量。我哪里出错了?

最佳答案

  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?php tag.
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();).
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
  5. Make sure you didn't delete or empty the session.
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere.
  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!).

PHP session lost after redirect

关于php - php session 变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com