页面之间的会话被清除

发布于 2024-10-01 15:04:46 字数 508 浏览 2 评论 0原文

我正在制作一个登录页面,由于某种原因,会话不会在我设置它的位置和我转发到的页面之间持续存在。我可以注释掉初始化会话的页面中的标题,并看到会话中包含数据。但是,当我在目标页面中执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-10-08 15:04:46

设置.php:

session_start();
$_SESSION['auth'] = true;

header('Location: /');

索引.php:

session_start();

var_dump($_SESSION);

创建这两个文件并请求 set.php。你看到了什么?

set.php:

session_start();
$_SESSION['auth'] = true;

header('Location: /');

index.php:

session_start();

var_dump($_SESSION);

Create these 2 files and request set.php. What do you see?

北座城市 2024-10-08 15:04:46

如果您设置了会话变量,然后进行标头重定向,则需要在重定向之前添加 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.

心意如水 2024-10-08 15:04:46

我经常遇到的情况是意外地从带有“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.'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文