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.
The most common method is to set
The Code is as follows: |
Copy code |
<? Php If (! Isset ($ _ SESSION ['last _ access']) | (time ()-$ _ SESSION ['last _ access'])> 60) $ _ SESSION ['last _ access'] = time (); ?> |
This is done. If you want to set an expiration date, you can also implement the instance in the program.
The Code is as follows: |
Copy code |
<? Php Unset ($ _ SESSION ['last _ access']) or $ _ SESSION ['last _ access'] = ''; ?> |
The session has an expiration mechanism.
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 gc_divisor values are 1 and 100, respectively.
The probability of gc running in the session is 1% for the numerator and denominator respectively. If you modify these two values, the efficiency of php is reduced. So this method is not correct !!
Therefore, modifying the gc_maxlifetime variable in the php. ini file can prolong the session expiration time: (for example, we can change the expiration time to 86400 seconds)
The Code is as follows: |
Copy code |
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 Code is as follows: |
Copy code |
# 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