In apache and php environments, the default expiration time is about 20 minutes. how can we set the session expiration time? The following is a summary of the specific methods. We usually set in the php program
In apache and php environments, the default expiration time is about 20 minutes. how can we set the session expiration time? The following is a summary of the specific methods.
We usually set it in a php program. The code is as follows:
-
- If (! Isset ($ _ SESSION ['last _ access']) | (time ()-$ _ SESSION ['last _ access'])> 60)
- $ _ SESSION ['last _ access'] = time ();
- ?>
This is done. if you want to set expiration, you can also implement it in the program. The code is as follows:
-
- Unset ($ _ SESSION ['last _ access']) or $ _ SESSION ['last _ access'] = '';
- ?>
Mechanism of session validity expiration
Session. gc_maxlifetime the original session expiration is a small probability event, respectively using session. gc_probability and session. gc_divisor is used to determine the probability of gc sessions in the running session. gc_probability and session. the default values of gc_divisor are 1 and 100, respectively, the numerator and denominator. Therefore, the probability of running gc in the session is 1%. if you modify these two values, the efficiency of php is reduced, so this method is not correct!
Therefore, modify the gc_maxlifetime variable in the php. ini file to extend the expiration time of the session: (for example, modify the Expiration Time to 86400 seconds). The code is as follows:
Session. gc_maxlife time = 86400
Then, restart your web service (generally apache.
When will session "recycle" occur?
By default, every php request will have a 1/100 probability of recovery, so it may be simply understood as "every 100 php requests will be recycled once ", this probability is controlled by the following parameters:
- # The probability is gc_probability/gc_divisor.
- Session. gc_probability = 1
- Session. gc_divisor = 100
Note 1: assume that gc_maxlifetime = 120. if the last modification time of a session file is before 120 seconds, before the next recovery (1/100 probability, this session is still valid.
Note 2: If your session is saved elsewhere in session. save_path, the session recycle mechanism may not automatically process expired session files. In this case, you need to manually (or crontab) delete expired sessions: cd/path/to/sessions; find-cmin + 24 | xargs rm