会话变量无法正常工作的PHP [英] Session variables not working php
本文介绍了会话变量无法正常工作的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的登录页面的代码,其中登录脚本检查用户的真实性,然后使用标题功能重定向到收件箱页面.
Here are the code of my login page where the login script checks for the authenticity of the user and then redirects to inbox page using header function.
<?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页面如下:
The inbox.php page looks like this:
<?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 =您必须登录才能看到此页面. 这意味着没有设置会话变量,或者inbox.php无法检索会话变量.我要去哪里错了?
Now with the above code, the inbox.php always shows the output: SESSION=you must log in to see this page. Which means that either the session variable is not being setup or the inbox.php is unable to retrieve the session variable. Where am i going wrong?
推荐答案
- 确保在调用任何会话之前调用了
session_start();
.因此,安全的选择是将其放在页面的开头,紧接着在打开<?php
标记之后,然后放在其他任何地方.另外,请确保在打开<?php
标记之前没有空格/制表符. - 在
header
重定向之后,使用exit();
结束当前脚本(其他人也建议使用session_write_close();
和session_regenerate_id(true)
,您也可以尝试使用它们,但我会使用exit();
). - 确保在用于测试它的浏览器中启用了cookie.
- 确保已关闭
register_globals
,您可以在php.ini
文件上也可以使用phpinfo()
进行检查.请参阅此,以了解如何打开它关闭. - 确保您没有删除或清空会话.
- 确保
$_SESSION
超全局数组中的键不会在任何地方被覆盖. - 确保您重定向到相同的域.因此,从
www.yourdomain.com
重定向到yourdomain.com
不会继续进行会话. - 确保您的文件扩展名是
.php
(确实如此!).
- 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. - After the
header
redirect, end the current script usingexit();
(Others have also suggestedsession_write_close();
andsession_regenerate_id(true)
, you can try those as well, but I'd useexit();
). - Make sure cookies are enabled in the browser you are using to test it on.
- Ensure
register_globals
is off, you can check this on thephp.ini
file and also usingphpinfo()
. Refer to this as to how to turn it off. - Make sure you didn't delete or empty the session.
- Make sure the key in your
$_SESSION
superglobal array is not overwritten anywhere. - Make sure you redirect to the same domain. So redirecting from a
www.yourdomain.com
toyourdomain.com
doesn't carry the session forward. - Make sure your file extension is
.php
(it happens!).
这篇关于会话变量无法正常工作的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文