PHP session_id() 不接受现有会话 ID
我在强制 PHP 会话重新启动时遇到问题。问题是:
我可以使用 session_id()
获取会话 ID,复制它,然后将其添加到脚本的最顶部:
session_id('the_session_id');
session_start();
当我打开新浏览器时,来自其他浏览器的会话是没有结转。我可以检查哪些设置?
I'm having trouble forcing sessions to restart in PHP. Here's the problem:
I can get my session id with session_id()
, copy it, and add to the very top of my script:
session_id('the_session_id');
session_start();
And when I open a new browser, the session from the other browser is not carried over. What settings can I check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因:
如果您关闭浏览器窗口并再次打开它,则此时会使用不同的 ID 启动第二个会话,如果所使用的 Web 应用程序具有某种基于会话的身份验证系统,则用户必须登录再次。同时用户必须注销两次!
解决方案:
此函数将使用真实的 cookie 作为会话 ID,并在每次脚本执行时更新过期时间。过期时间等于 PHP 指令“gc_maxlifetime”(默认)或每个自定义值。因此,将此函数放入您的 PHP 文件中。我们会需要它。
现在,在您要发出
session_id('the_session_id');
和session_start();
的页面顶部,删除这些行并使用下面的代码启动会话:启动一个由 php 配置指定的过期时间的会话
启动一个将在 1 小时后过期的会话:
Reason:
If you close the browser window and open it again, then at this moment a second session is started with a different ID, if the used web application has some session based authentication system the user has to login again. At the same time the user has to logout twice!
Solution:
This function will use a real cookie for the session ID and updates the expiration time with every script execution. The expiration is equal to the PHP directive "gc_maxlifetime" (default) or every custom value. So, put this function in your PHP file. We will need it.
Now, in the top of your page where you're issuing
session_id('the_session_id');
andsession_start();
, remove those lines and start session with this code below:To start a session with an expire time given by the php configuration
To start a session that will expire in 1 hour: