问题
I have an upload form that takes a user about 30 min. to complete. The whole time they're idle on this page. I use the ini_set() function. You can see the top of the upload page here:
<?php
session_start();
ini_set('session.gc_maxlifetime',10080);
?>
What keeps happening is if a user is idle for 30 or more min. the data gets truncated and the User_id is recorded as a NULL value. I'm completely baffled how this is happening. I initially set the maxlifetime in the php.ini but that overloaded the cached session data. So, I'm just using ini_set. Why would the data be getting messed up and the user_id not be getting recorded. Note: if you do the form fast enough there's no issues.
回答1:
You should use ini_set()
before the session_start()
.
回答2:
That's because there are another scripts called with session.gc_maxlifetime
value ~30 minutes. So other scripts' session garbage collector clears the out of time sessions (from their perspective the sessions are timed out).
The solution: you need to change the global value of session.gc_maxlifetime
in php.ini
or .htaccess
or specify the same (or at least larger) value in every script.
回答3:
Setting
session.gc_maxlifetime
withini_set
does not necessarily have effect on the way the server purges the sessions. For instance, on Debian (and, i imagine on some other systems as well), the sessions are being purged by a cron job that is not affected by any configuration you do inside the script (it only reads global php.ini settings). You can increase this server-wide in php.ini if that is the case.You can override session save handler and then deal with session expiration yourself...
you can use some ajax keepalive calls so that even when the user is idle on that form page there is something "talking" to the server within the expiration timeframe to keep the session alive
来源:https://stackoverflow.com/questions/9915531/user-session-keeps-expiring