我正在尝试增加会话超时时间

发布于 2024-10-17 15:37:16 字数 300 浏览 1 评论 0原文

我正在尝试增加 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 技术交流群。

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

发布评论

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

评论(6

猫卆 2024-10-24 15:37:16
ini_set('session.gc_probability',1); 
ini_set('session.gc_divisor',1);

这两个命令强制 PHP 对站点上的每次点击运行会话清理脚本。 PHP 运行 gc 的公式是:

if (random() < (gc_probability / gc_divisor)) then
      run_session_garbage_collector()
}

现在你得到了 1/1,所以垃圾收集器运行的机会是 100%。虽然延长超时时间很好,但您还希望减少收集器运行的机会。否则,您将强制 PHP 对所有会话文件进行全面扫描,并针对站点上的每次点击解析每个会话文件,以查找可能已过期的一两个文件。

尝试将 gc_probability 设置为 0 以完全禁用垃圾收集器。

另外,请注意,在使用 ini_set() 的脚本中更改设置不会更改其他脚本使用的超时/默认值。该脚本可能有更长的超时时间和更改的概率,但如果其他脚本有默认超时(10 分钟或其他),那么它们会很乐意删除任何“陈旧”会话。

设置会话超时/清理设置的正确位置是在 php.ini 级别,以便它适用于站点上的所有脚本,而不仅仅是您的这个脚本。

ini_set('session.gc_probability',1); 
ini_set('session.gc_divisor',1);

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:

if (random() < (gc_probability / gc_divisor)) then
      run_session_garbage_collector()
}

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.

梦中的蝴蝶 2024-10-24 15:37:16

您可以尝试在 php.ini 文件中设置 session.gc_maxlifetime = 12*60*60 。
否则,当脚本结束时, session.gc_maxlifetime 变量每次都会被重置。

来自 php.net/ini_set :

字符串 ini_set ( 字符串 $varname , 字符串 $newvalue )

设置给定配置选项的值。配置选项将在脚本执行期间保留这个新值,并将在脚本结束时恢复。

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 :

string ini_set ( string $varname , string $newvalue )

Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

将军与妓 2024-10-24 15:37:16

会话 cookie 也可能即将过期:会话。 cookie 生命周期

The session cookie might be expiring as well: session.cookie-lifetime

感悟人生的甜 2024-10-24 15:37:16

我不确定“私人站点”是否是指它单独位于专用服务器上,但如果不是:

如果您的会话临时文件与其他站点的 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.

一抹淡然 2024-10-24 15:37:16

session.gc_divisor 与 session.gc_probability 结合定义了每次会话初始化时启动 gc(垃圾收集)进程的概率。概率是通过 gc_probability/gc_divisor 计算的,例如 1/100 表示每个请求有 1% 的机会启动 GC 进程。 session.gc_divisor 默认为 100。

所以你在做 1/1 - 我不确定它实际上是如何运作的,但这可能是奇怪的行为。

session.gc_divisor coupled with session.gc_probability defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. session.gc_divisor defaults to 100.

So your doing 1/1 - I'm not sure how that will actually function, but it could be odd behavior.

情深如许 2024-10-24 15:37:16

希望这会对您有所帮助。只需将以下代码复制并粘贴到 web.config 文件中

<sessionState mode="InProc" stateNetworkTimeout="10" sqlCommandTimeout="30" cookieName="ASP.NET_SessionId" timeout="53200" regenerateExpiredSessionId="false">
    <providers><clear/></providers>
</sessionState>

Hope this will help you. just copy and paste the bellow code in web.config file

<sessionState mode="InProc" stateNetworkTimeout="10" sqlCommandTimeout="30" cookieName="ASP.NET_SessionId" timeout="53200" regenerateExpiredSessionId="false">
    <providers><clear/></providers>
</sessionState>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文