且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP 会话超时 htaccess 文件

更新时间:2023-12-04 20:13:46

PHP 不会像您认为的那样处理会话 cookie...

PHP doesn't do with session cookies what you might think it does...

php_value session.gc_maxlifetime "86400"

这只是设置垃圾收集可能发生的时间 - 因为在此时间之后会话数据被垃圾收集的可能性很小,这不太可能成为罪魁祸首.

This merely sets the time after which garbage collection may occur - since there's only a very small chance that the session data will be garbage collected after this time this is unlikely to be the culprit.

php_value session.cookie_lifetime "86400"

这会设置会话 cookie 过期的时间...但是它不会在 session_start() 更新,因此,如果您在一页上开始会话,设置时钟滴答作响.从页面移动到页面不会重置时钟",因此 cookie_lifetime 为 600 的会话将在开始后 10 分钟到期而不是在 10 分钟不活动后 -您可能会看到这种行为.

This sets the time at which the session cookie expires... but it doesn't update at session_start() therefore, if you start the session on one page, that sets the clock ticking. Moving from page to page won't reset the "clock" so a session with a cookie_lifetime of 600 will expire 10 minutes after it was started not after 10 minutes of inactivity - you may be seeing this behaviour.

***的办法是设置 php_value session.cookie_lifetime "0" 这意味着会话 cookie 将在用户关闭浏览器时过期.

Your best bet is to set php_value session.cookie_lifetime "0" this means that the session cookie will expire only when the user closes their browser.

如果您想处理在一段时间不活动后触发的任意到期,***在 PHP 代码中通过在会话中设置一个变量来实现,例如 $_SESSION['expires'] 并在每个页面的开头检查/更新/处理它.

If you want to handle an arbitrary expiry that triggers after a period of inactivity it's best to do that in the PHP code by setting a variable within the session, something like $_SESSION['expires'] and checking/updating/handling that at the start of every page.