我正在尝试增加会话超时时间
我正在尝试增加 SESSION 时间,以便用户在 12 小时内不会超时。这是一个仅供员工使用的私人站点,超时很烦人,并且会导致部分填写的数据丢失。根据我读到的内容,以下代码应该可以工作:
ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_probability',1);
ini_set('session.gc_divisor',1);
session_start();
但没有效果。有什么想法吗?
谢谢
I am trying to increase the SESSION time so that the users do not time out for 12 hours. This is a private site used only by employees and the timeout is annoying and it causes partially filled out data to be lost. According to what I read the following code should work:
ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_probability',1);
ini_set('session.gc_divisor',1);
session_start();
but it has no effect. Any thoughts?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这两个命令强制 PHP 对站点上的每次点击运行会话清理脚本。 PHP 运行 gc 的公式是:
现在你得到了 1/1,所以垃圾收集器运行的机会是 100%。虽然延长超时时间很好,但您还希望减少收集器运行的机会。否则,您将强制 PHP 对所有会话文件进行全面扫描,并针对站点上的每次点击解析每个会话文件,以查找可能已过期的一两个文件。
尝试将 gc_probability 设置为 0 以完全禁用垃圾收集器。
另外,请注意,在使用 ini_set() 的脚本中更改设置不会更改其他脚本使用的超时/默认值。该脚本可能有更长的超时时间和更改的概率,但如果其他脚本有默认超时(10 分钟或其他),那么它们会很乐意删除任何“陈旧”会话。
设置会话超时/清理设置的正确位置是在 php.ini 级别,以便它适用于站点上的所有脚本,而不仅仅是您的这个脚本。
These two are forcing PHP to run the session cleanup script for EVERY hit on your site. PHP's formula to run the gc is:
Right now you've got 1/1, so 100% chance of the garbage collector being run. While extending the timeout period is good, you also want to REDUCE the chance the collector runs at all. Otherwise you're forcing PHP to do a full scan of ALL session files and parse EACH one, for EVERY hit on your site, to find the odd one or two that might have expired.
Try setting the gc_probability to 0 to completely disable the garbage collector.
As well, be aware that changing the settings, as you are, within a script with ini_set() doesn't change the timeouts/defaults that OTHER scripts use. This script might have a longer timeout and changed probability, but if other scripts hav ethe default timeout (10 minutes or something), then they'll happily nuke any "stale" sessions.
The proper place to set session timeout/cleanup settings is at the php.ini level, so that it applies to all scripts on the site, not just this single script of yours.
您可以尝试在 php.ini 文件中设置 session.gc_maxlifetime = 12*60*60 。
否则,当脚本结束时, session.gc_maxlifetime 变量每次都会被重置。
来自 php.net/ini_set :
You could try setting the session.gc_maxlifetime = 12*60*60 in your php.ini file.
Otherwise when your script ends the session.gc_maxlifetime variable will be reset each time.
from php.net/ini_set :
会话 cookie 也可能即将过期:会话。 cookie 生命周期
The session cookie might be expiring as well: session.cookie-lifetime
我不确定“私人站点”是否是指它单独位于专用服务器上,但如果不是:
如果您的会话临时文件与其他站点的 tmp 文件存储在同一目录中,另一个网站上较短的会话生命周期可能会触发垃圾收集,这可能删除您的“私人网站”会话。取决于服务器设置。就我个人而言,我为每个客户端都有一个 tmp 文件夹,以避免类似的情况。
I'm not sure if by "private site" you mean its alone on a dedicated server, but if not:
If your session temp files are being stored in the same directory as other sites' tmp files, its possible that a lower session lifetime on another website is triggering garbage collection, which could delete your "private site's" session. Depends on server setup. Personally I've got a tmp folder for each client to avoid things like that.
所以你在做 1/1 - 我不确定它实际上是如何运作的,但这可能是奇怪的行为。
So your doing 1/1 - I'm not sure how that will actually function, but it could be odd behavior.
希望这会对您有所帮助。只需将以下代码复制并粘贴到 web.config 文件中
Hope this will help you. just copy and paste the bellow code in web.config file