PHP会话不能从Cookie恢复 [英] PHP Session Not Restoring from Cookies
问题描述
当用户返回到我的网站时,它会尝试从 $ _ COOKIE
关联数组中恢复上一个会话。它不能按预期工作。我可以查看我的浏览器的cookie管理器,看到cookie是有的,但它们似乎没有保存到 $ _ SESSION
关联数组。
When a user returns to my website, it attempts to restore their last session from the $_COOKIE
associative array. It's not working as expected. I can look in my browser's cookie manager and see that the cookies are there, but they don't seem to be getting saved to the $_SESSION
associative array.
这是用户返回我的网站时的程序流程:
This is essentially the program flow when a user returns to my site:
foreach ( $_COOKIE as $name => $val )
{
$_SESSION[$name] = $val;
}
session_start();
...
$some_var = $_SESSION[$var_name];
我有事情乱序,或者我不应该覆盖 PHPSESSID
?任何洞察,我做错了,将不胜感激。感谢。
Do I have things out of order, or should I not be overwriting PHPSESSID
? Any insight as to what I'm doing wrong would be appreciated. Thanks.
推荐答案
您的会话和Cookie混在一起。你不需要把东西放到$ _COOKIE数组中。只需使用session_start(),然后把东西放到$ _SESSION。 PHP将自动为您管理会话/ cookie。
You're getting sessions and cookies mixed up. You don't need to put things into the $_COOKIE array. Just use session_start() and then put things into $_SESSION. PHP will automatically then manage the session/cookie for you.
$ _ COOKIE变量存储在用户浏览器上,因此它们不安全,可以由用户操作=>安全风险。
$_COOKIE variables are stored on the users browser, so they aren't secure and can be manipulated by the user => security risk.
$ _ SESSION变量只存储在服务器上。存储在cookie中的唯一的是session_id,所以$ _SESSION变量不能被操作。
$_SESSION variables are stored only on the server. The only thing stored in the cookie is a session_id, so $_SESSION variable can't be manipulated.
这是否有意义?
这篇关于PHP会话不能从Cookie恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!