PHP 会话超时 htaccess 文件 [英] PHP session timeout htaccess file

查看:42
本文介绍了PHP 会话超时 htaccess 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,但目前正在解决会话超时问题.基本上,即使我更改了 session.gc_maxlifetime,我的会话也会一直超时.我认为另一个使用相同目录来存储没有设置 maxlifetime 的会话数据的脚本正在运行,因此它将使用较短的值.为了解决这个问题,我改变了 htaccess 文件,但它在 1 小时 30 分钟后仍然终止,我需要它持续更长时间.我的 htaccess 文件在下面.我已经查看并尝试了该板上的许多相关帖子,但到目前为止没有任何效果.任何想法将不胜感激.

Hi i'm new to programming but am currently working on a session timeout problem. Basically my session keeps timing out even though i've changed the session.gc_maxlifetime. I think another script using the same directory to store session data which doesn't have the maxlifetime set, is running and thus it'll use the shorter value instead. TO combat this i've altered the htaccess file but it is still terminating after an 1hr 30mins, i need it to last for longer. My htaccess file is below. I've looked at and tried many of the relevant posts on this board but nothing has worked so far. Any ideas would be greatly appreciated.

SetEnv PHPRC /home/rocket/public_html/php.ini

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<IfModule mod_php5.c>
php_value session.save_path "new/username/php_sessions"
php_value session.gc_maxlifetime "86400"
php_value session.cookie_lifetime "86400"

</IfModule>


# END WordPress

推荐答案

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.

这篇关于PHP 会话超时 htaccess 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆