页面之间的会话被清除
我正在制作一个登录页面,由于某种原因,会话不会在我设置它的位置和我转发到的页面之间持续存在。我可以注释掉初始化会话的页面中的标题,并看到会话中包含数据。但是,当我在目标页面中执行 print_r 时,会话为空。
我已经确保调用了 session_start 。该网站只有一个域,并且我的浏览器设置为接受 cookie。我可以转发到任何其他页面并查看会话数据,但不能转发到此页面。
有人可以提供一些东西来帮助调试这个吗?
$_SESSION['auth'] = $auth;
header( "Location: /" ); // commenting this out shows the data is in fact there
我想保护索引页,因此我测试是否设置了 session['auth'] 。如果没有,我将转发到 /user/login,它允许用户登录。如果成功,那么我们将转发回索引页面,其中应该通过 isset 会话测试。但它失败了并且没有会话数据。
I'm making a login page and for some reason the session will not persist between where I set it and the page where I am forwarding to. I can comment out the header in the page where the session was initialized and see that the session has the data in it. However, when I do a print_r in the target page, the session is empty.
I have already made sure that session_start is called. There is only one domain for this site and my browser is set to accept cookies. I can forward to any other page and see the session data but just not this one.
Is there something that someone can offer to help in debugging this?
$_SESSION['auth'] = $auth;
header( "Location: /" ); // commenting this out shows the data is in fact there
I want to protect the index page so I test to see if session['auth'] is set. If not, I forward over to /user/login which allows the user to login. If successful then we forward back over to the index page where it should pass the isset session test. It fails though and there is no session data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置.php:
索引.php:
创建这两个文件并请求 set.php。你看到了什么?
set.php:
index.php:
Create these 2 files and request set.php. What do you see?
如果您设置了会话变量,然后进行标头重定向,则需要在重定向之前添加 session_write_close() ,否则您将丢失会话修改。
If you set a session variable, then do a header redirect, you need to add session_write_close() before the redirect or you will lose your sesson modification.
我经常遇到的情况是意外地从带有“www”的页面重定向。在页面的 URL 中没有。我不太确定为什么会发生这种情况,但由于某种原因,有或没有“www.”的站点之间的会话是不同的。
Something that I've ran in to quite a bit is accidentally redirecting from a page with 'www.' in the URL to a page without. I'm not exactly sure why it happens but for some reason the session between a site is different with and without the 'www.'.