PHP 会话在标头后被破坏/丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2037316/
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
PHP session destroyed / lost after header
提问by Ben Everard
I've got a script that sets some session values before redirecting to /using header().
我有一个脚本,可以在重定向到/using之前设置一些会话值header()。
I've read many posts about the $_SESSIONvariable being destroyed / lost after header(), even after I implemented this:
我已经阅读了很多关于$_SESSION变量在 之后被销毁/丢失的帖子header(),即使在我实现了这个之后:
// set session here
session_regenerate_id(true);
session_write_close();
header("Location: /");
session_start()is set in the correct places, does anyone know of anything that I might be missing?
session_start()设置在正确的位置,有人知道我可能遗漏了什么吗?
On the index.php page I have this:
在 index.php 页面上我有这个:
session_start();
print_r($_SESSION);
// outputs nothing :'(
The code is pretty complex so will not post it all, just snippets.
代码非常复杂,所以不会全部发布,只是片段。
采纳答案by Ben Everard
In the interest of closing this question, we had concluded it was a problem with the server configuration, not surprising considering the host is well known for this kind of thing.
为了结束这个问题,我们得出结论,这是服务器配置的问题,考虑到主机以这种事情而闻名,这并不奇怪。
回答by John Parker
I've never seen any session related issues due to using location headers - are you sure you're calling session_starton both pages?
由于使用位置标头,我从未见过任何与会话相关的问题 - 您确定要在两个页面上调用session_start吗?
Hmm... this answer made a lot more sense before you added the session_start bits above, and mentioned the fact that you were sure you were using session_start. :-)
嗯...在您添加上面的 session_start 位之前,这个答案更有意义,并提到您确定使用 session_start 的事实。:-)
回答by streetparade
header must be sent before session close
头必须在会话关闭之前发送
session_regenerate_id(true);
header("Location: /");
// the header must be sent before session close
session_write_close(); // here you could also use exit();
回答by junaid
just put exit; after header :D I solved by this
只需退出;标题后:DI由此解决
回答by vivek goyal
After the Header redirect you need to exit the PHP script:
在 Header 重定向之后,您需要退出 PHP 脚本:
header("Location: /");
exit();
回答by arc_shiva
You don't need to start session_start() in each page. cuz untill your browser is closed the same session remains for the entire path you have specified in php.ini
您不需要在每个页面中启动 session_start()。因为直到您的浏览器关闭,您在 php.ini 中指定的整个路径都会保留相同的会话

