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:
Any idea why this isn't working? (PHP 5.3.10)
Thanks
我知道这个问题已经在SO中提出了,我已经尝试了解决方案,但它仍然没有修复。 / p>
PHP在一段时间不活动后删除会话(我假设是24分钟,因为它是默认值并且似乎适合测试)。 p>
我在所有页面中都设置了以下代码: p>
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按预期方式
code> pre>
但会话在24分钟(或左右)不活动后仍会重置。 p>
phpinfo()为会话返回以下输出:
p>
知道为什么这不起作用? (PHP 5.3.10) p>
谢谢 p>
div>
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!!
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.