一段时间后PHP重置会话 [英] PHP resetting Session after some time

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

问题描述

我知道此问题已在SO中提出,我已经尝试了解决方案,但仍未解决.

I know this problem has been presented here in SO and I've tried the solutions but it's still not fixed.

PHP将在闲置一段时间后删除会话(我假设这是默认的24分钟,并且似乎符合测试要求).

PHP is deleting the session after some time of inactivity (i assume 24 minutes as it's the default and seems to fit the testing).

我在所有页面上都设置了以下代码:

I have the following code set in all the pages:

ini_set('display_errors', 0);
$sessionCookieExpireTime = 2880000;
session_set_cookie_params($sessionCookieExpireTime);
ini_set('session.gc_maxlifetime', $sessionCookieExpireTime);

session_start();
echo ini_get('session.gc_maxlifetime'); //echos 2880000 as expected

但是在闲置24分钟(或大约24分钟)后,会话仍会重置.

But the session still gets reset after 24 minutes (or so) of inactivity.

phpinfo()返回会话的以下输出:

phpinfo() return the following output for session:

有人知道为什么这行不通吗? (PHP 5.3.10)

Any idea why this isn't working? (PHP 5.3.10)

谢谢

推荐答案

尽管Marc B答案分享了一些很好的见解,但对我来说并不奏效.我很确定自己的脚本一切都很好,并且代码中的会话没有任何混乱.

Although Marc B answer shares some great insight it wasn't working for me. I was pretty sure everything was fine with my script and I had nothing messing with the session in my code.

经过一番史诗般的奋斗之后,我发现我的问题实际上是由于共享托管环境引起的.从PHP文档:

After an epic struggle I discovered that my problem was actually due to shared hosting environment. From the PHP doc:

如果不同的脚本……共享用于存储会话的相同位置 数据,然后具有最小值的脚本将[确定 会话超时]".

"If different scripts … share the same place for storing the session data then the script with the minimum value will [determine the session timeout]".

此后,问题就很明显了.某些脚本(托管在同一服务器上)正在使用默认的php.ini session.gc_maxlifetime,并且正在重置我的会话.

After this the problem was quite obvious. Some script (hosted on the same server) was using the default php.ini session.gc_maxlifetime and that was resetting my sessions.

解决方案是在我的主机的根目录下创建一个文件夹(确保它不能通过网络访问),为其设置正确的权限,然后使用session.save_path告诉php将我的会话存储在哪里.像这样:

The solution was to create a folder under the root of my hosting (make sure it's not web accessible), set the right permissions to it and then use session.save_path to tell php where to store my sessions. Something like:

ini_set("session.gc_maxlifetime","21600"); // 6 hours
ini_set("session.save_path", "/your_home/your_sessions/");
session_start();

该网站提供了深刻的见解:共享主机上的php会话

This website provided great insight: php sessions on shared hosting

因此,如果遇到此问题,请确保您遵循Marc B的建议,如果不起作用,请尝试此操作.

So if you come accross this issue make sure you follow Marc B recommendations and if that doesn't work try this out.

最美好的祝愿!

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

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