PHP 会话不会在每个请求上延长 Cookie 过期时间

编程入门 行业动态 更新时间:2024-10-27 14:34:19
本文介绍了PHP 会话不会在每个请求上延长 Cookie 过期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

session_start() 是否应该通过 session.gc_maxlifetime 变量延长会话 ID cookie 的寿命?

Is session_start() supposed to extend the life of the session ID cookie by the session.gc_maxlifetime variable?

我的 session.gc_maxlifetime 是 24 分钟,每次会话只有 24 分钟,而不管网站上是否有其他活动.我得到我的会话,刷新页面,过期时间没有改变.无论如何,这都会导致在登录 24 分钟后注销.我的配置有问题吗?

My session.gc_maxlifetime is 24 minutes, and each session is only living 24 minutes regardless of additional activity on the site. I get my session, refresh the page, and the expiration time does not change. This results in a logout after 24 minutes of login, no matter what. Is there something wrong with my configuration?

推荐答案

我认为这篇文章将提供您正在寻找的解决方案:PHP 中的会话超时:最佳实践

I think this post will provide the solution you are looking for: Session timeouts in PHP: best practices

基本上,当 session_start() 被调用时,有 1% 的概率(默认情况下)会运行垃圾收集器.当垃圾收集器运行时,它会扫描并删除过期的会话.但是,当您是访问该页面的唯一用户(在开发期间您可能是)或用户很少时,垃圾收集器只会在您访问页面时运行.这发生在 session_start() 被调用之后,有效地重置了计时器.不要试图解决这个问题,只需实现您自己的 session_start() 函数来强制超时.试试我上面给你的链接中 @Glass Robot 发布的功能.

Basically, when session_start() is called, there is a 1% probability (by default) that the garbage collector will be run. When the garbage collector is run it scans for and deletes expired sessions. However, when you are the only user accessing the page (which you probably are, during development) or there are very few users, the garbage collector will only run when you access a page. This happens AFTER session_start() is called, effectively resetting the timer. Instead of trying to work around this, just implement your own session_start() function which enforces the timeout. Try the function that the @Glass Robot posted, in the link I gave you above.

更多推荐

PHP 会话不会在每个请求上延长 Cookie 过期时间

本文发布于:2023-10-29 10:42:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   在每个   PHP   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!