dongshenjie3055 2012-09-03 16:44
浏览 33
已采纳

PHP重置会话一段时间后

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

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

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

phpinfo() return the following output for session: enter image description here

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

Thanks

  • 写回答

2条回答 默认 最新

  • duanfu9523 2012-09-06 02:14
    关注

    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.

    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]“.

    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.

    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();
    

    This website provided great insight: php sessions on shared hosting

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

    Best wishes!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?