Many people on the Internet have given the answer: modify session.gc_maxlifetime in the configuration file. If you want to know more about the session recycling mechanism, continue reading. (The environment of this article is php5.2)
Overview: Every php request has a 1/100 probability (default value) of triggering "session recycling". If "session recycling" occurs, the /tmp/sess_* files will be checked. If the last modification time exceeds 1440 seconds (the value of gc_maxlifetime), they will be deleted, which means that these sessions have expired.
1. How does the session exist on the end (usually with PHP module)?
By default, php will save the session in the /tmp directory, and the file name will be like this: sess_01aab840166fd1dc253e3b4a3f0b8381. Each file corresponds to a session.
more /tmp/sess_01aab840166fd1dc253e3b4a3f0b8381
username|s:9:”jiangfeng”;admin|s:1:”0″;
#Variable name|type:length:value
Delete The session file here means that the corresponding session has expired.
2. How does session exist on the client side (usually the browser)?
Session is on the browser side, you only need to save the session ID (the unique ID generated by the server side). There are two ways to save it: in cookies and in URLs. If the session ID is saved in the cookie, you can see that there is a PHPSESID variable in the browser's cookie. If it is passed by URL, you can see the URL in the form:
index.php?PHPSESID=01aab840166fd1dc253e3b4a3f0b8381. (On the server side, use session.use_cookies to control which method is used)
3. On the server side, how does PHP determine whether the session file has expired?
If the "last modification time" to "now" exceeds gc_maxlifetime (default is 1440) seconds, the session file is considered expired. When the next session is recycled, if the file has not been If changed, the session file will be deleted (the session will expire).
To put it simply, if I log in to a website and there is no operation within 1440 seconds (default value), then the corresponding session is considered to have expired.
So, modifying the gc_maxlifetime variable in the php.ini file can extend the session expiration time: (for example, we change the expiration time to 86400 seconds)
session.gc_maxlifetime = 86400
Then, just restart your web service (usually apache).
Note: In php5, session expiration uses a recycling mechanism. The time set here is 86400 seconds. If the session has not been modified within 86400 seconds, it will not be deleted until the next "recycling".
3. When does session "recycling" occur?
By default, for every php request, there will be a 1/100 probability of recycling, so it may be simply understood as "one recycling occurs for every 100 php requests". 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 this situation gc_maxlifetime=120, If a session file was last modified 120 seconds ago, the session will still be valid until the next recycling (1/100 probability) occurs.
Note 2: If your session uses session.save_path to save the session elsewhere, the session recycling mechanism may not automatically process expired session files. At this time, you need to delete expired sessions manually (or crontab) regularly: cd /path/to/sessions; find -cmin +24 | xargs rm
4. Some special circumstances
due to recycling The mechanism will check the "last modification time" of the file, so if a session is active, but the content of the session has not changed, then the corresponding session file has not changed, and the recycling mechanism will consider this to be a period of inactivity for a long time. session and delete it. This is something we don’t want to see. We can solve this problem by adding the following simple code: