dongshenjie3055
dongshenjie3055
2012-09-03 16:44

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 duanfu9523 9年前

    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!!

    点赞 评论 复制链接分享
  • doubi9999 doubi9999 9年前

    Are you doing this code in EVERY script that uses sessions? ini_set changes apply ONLY to the script they're executed in, and ONLY for the execution lifetime of that particular script.

    If you want to make it a permanent global change, you'll have to modify php.ini, or put some php_values directives into http.conf/.htaccess.

    点赞 评论 复制链接分享

为你推荐