php 会话变量不起作用php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19692157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Session variables not working php
提问by Arihant
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!");
}
?>
The inbox.php page looks like this:
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
?>
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?
现在有了上面的代码,inbox.php 总是显示输出:SESSION=you must login to see this page。这意味着会话变量未设置或 inbox.php 无法检索会话变量。我哪里出错了?
回答by revo
- 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 thisas 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!).
- 确保
session_start();
在调用任何会话之前调用。所以一个安全的赌注是把它放在你的页面的开头,紧跟在<?php
其他任何东西之前的开始标签之后。还要确保开始<?php
标记之前没有空格/制表符。- 在后
header
重定向,使用结束当前的脚本exit();
(其他人也建议session_write_close();
,并session_regenerate_id(true)
,你可以尝试那些为好,但我会用exit();
)。- 确保在您用来测试的浏览器中启用了 cookie。
- 确保
register_globals
已关闭,您可以在php.ini
文件上进行检查,也可以使用phpinfo()
. 请参阅本关于如何将其关闭。- 确保您没有删除或清空会话。
- 确保您的
$_SESSION
超全局数组中的键没有在任何地方被覆盖。- 确保重定向到同一个域。因此从 a 重定向
www.yourdomain.com
到yourdomain.com
不会将会话向前推进。- 确保您的文件扩展名是
.php
(它发生了!)。
回答by Vadman
I had the same issue for a while and had a very hard time figuring it out. My problem was that I had the site working for a while with the sessions working right, and then all of the sudden everything broke.
我有一段时间遇到了同样的问题,并且很难弄清楚。我的问题是我让网站工作了一段时间,会话正常工作,然后突然间一切都坏了。
Apparently, your session_save_path(), for me it was /var/lib/php5/, needs to have correct permissions (the user running php, eg www-data needs write access to the directory). I accidentally changed it, breaking sessions completely.
显然,您的 session_save_path(),对我来说是 /var/lib/php5/,需要具有正确的权限(运行 php 的用户,例如 www-data 需要对该目录的写访问权限)。我不小心更改了它,完全中断了会话。
Run sudo chmod -R 700 /var/lib/php5/
and then sudo chown -R www-data /var/lib/php5/
so that the php user has access to the folder.
运行sudo chmod -R 700 /var/lib/php5/
然后sudo chown -R www-data /var/lib/php5/
让 php 用户可以访问该文件夹。
回答by Luis Felipe de Melo
If you use a connection script, dont forget to use session_start();
at the connection too, had some trouble before noticing that issue.
如果您使用连接脚本,也不要忘记session_start();
在连接处使用,在注意到该问题之前遇到了一些麻烦。
回答by Ashraf Ali
Maybe if your session path is not working properly you can try session.save_path(path/to/any folder);
function as alternative path. If it works you can ask your hosting provider about default path issue.
也许如果您的会话路径无法正常工作,您可以尝试将其session.save_path(path/to/any folder);
用作替代路径。如果它有效,您可以向您的托管服务提供商询问默认路径问题。
回答by Jacek Dziurdzikowski
The other important reason sessions can not work is playing with the session cookie settings, eg. setting session cookie lifetime to 0 or other low values because of simple mistake or by other developer for a reason.
会话无法工作的另一个重要原因是使用会话 cookie 设置,例如。由于简单的错误或其他开发人员出于某种原因将会话 cookie 生存期设置为 0 或其他低值。
session_set_cookie_params(0)
回答by talsibony
I had similar issue and with the cookie domain:
我有类似的问题和 cookie 域:
ini_set('session.cookie_domain', '.domain.com');
the domain was setup wrong so all sessions were ignored because the user cookie was never set right hope this will help someone.
域设置错误,因此所有会话都被忽略,因为用户 cookie 从未设置正确,希望这对某人有所帮助。
回答by Arihant
Just talked to the hosting service, it was an issue at their end. he said " your account session.save_path was not set as a result issue arise. I set it for you now."
刚刚与托管服务商谈过,这是他们最后的问题。他说“你的帐户 session.save_path 没有设置,结果出现了问题。我现在为你设置了。”
And it works fine after that :)
在那之后它工作正常:)
回答by Aman Shukla
I was also facing the same problem i did the following steps to resolve the issue
我也遇到了同样的问题我做了以下步骤来解决这个问题
- I edited the file /etc/php.ini and searched the path session.save_path = "/var/lib/php/session" you have to give your session info
- 我编辑了文件 /etc/php.ini 并搜索了路径 session.save_path = "/var/lib/php/session" 你必须提供你的会话信息
2 After that just changed the permission given below *chown root.apache /var/lib/php/session * That's it. These above steps resolve my issue
2 之后,只需更改下面给出的权限 *chown root.apache /var/lib/php/session * 就是这样。以上这些步骤解决了我的问题
回答by Devqxz
I encountered this issue today. the issue has to do with the $config['base_url'] . I noticed htpp://www.domain.com and http://example.comwas the issue. to fix , always set your base_url to http://www.example.com
我今天遇到了这个问题。该问题与 $config['base_url'] 有关。我注意到 htpp://www.domain.com 和http://example.com是问题所在。要修复,请始终将 base_url 设置为http://www.example.com