Home >Backend Development >PHP Tutorial >PHP session validity session.gc_maxlifetime_PHP tutorial
A known and effective method is to use session_set_save_handler to take over all session management work. Generally, the session information is stored in the database, so that all expired sessions can be deleted through SQL statements and the validity period of the session can be accurately controlled. This is also a commonly used method for large websites based on PHP. However, for ordinary small websites, it seems that there is no need to work so hard.
But the general Session has a limited lifespan. If the user closes the browser, the Session variables cannot be saved! So how can we achieve the permanent life span of Session?
As we all know, the Session is stored on the server side. The user's file is obtained based on the SessionID provided by the client, and then the file is read to obtain the value of the variable. The SessionID can use the client's Cookie or the Query_String of the Http1.1 protocol (that is, (the part after the "?" in the accessed URL) is transmitted to the server, and then the server reads the directory of the Session...
To realize the permanent life of the Session, you first need to understand the relevant settings of the Session in php.ini (open php.ini file, in the "[Session]" section):
1. session.use_cookies: The default value is "1", which means that the SessionID is passed by Cookie, otherwise it is passed by Query_String;
2. session.name: This is the variable name stored in SessionID. It may be passed by Cookie or Query_String. The default value is "PHPSESSID";
3. session.cookie_lifetime: This represents the time that SessionID is stored in the client cookie. The default is 0, which means that the SessionID will be invalidated as soon as the browser closes it... It is because of this that the Session cannot be used permanently!
4. session.gc_maxlifetime: This is the time that Session data is stored on the server side. If this time is exceeded, the Session data will be automatically deleted!
There are many more settings, but these are the ones related to this article. Let’s start with the principles and steps of using permanent Session.
As mentioned before, the server reads Session data through SessionID, but generally the SessionID sent by the browser is gone after the browser is closed, so we only need to manually set the SessionID and save it, no...
If you have the permission to operate the server, setting this is very, very simple. You just need to perform the following steps:
1. Set "session.use_cookies" to 1 and turn on Cookie to store SessionID, but the default is 1. , generally no need to modify;
2. Change "session.cookie_lifetime" to positive infinity (of course there is no parameter for positive infinity, but there is no difference between 999999999 and positive infinity);
3. Set "session.gc_maxlifetime" It is the same time as "session.cookie_lifetime";
It is clearly stated in the PHP documentation that the parameter for setting the session validity period is session.gc_maxlifetime. This parameter can be modified in the php.ini file or through the ini_set() function. The problem is that after many tests, modifying this parameter basically has no effect, and the session validity period still maintains the default value of 24 minutes.
Due to the working mechanism of PHP, it does not have a daemon thread to regularly scan session information and determine whether it is invalid. When a valid request occurs, PHP will decide whether to start a GC (Garbage Collector) based on the value of the global variable session.gc_probability/session.gc_divisor (which can also be modified through the php.ini or ini_set() function).
By default, session.gc_probability = 1, session.gc_divisor = 100, which means there is a 1% probability that GC will be started. The job of the GC is to scan all session information, subtract the last modification time (modified date) of the session from the current time, and compare it with the session.gc_maxlifetime parameter. If the survival time has exceeded gc_maxlifetime, the session will be deleted.
So far, everything is working fine. So why does gc_maxlifetime become invalid?
By default, session information will be saved in the system's temporary file directory in the form of text files. Under Linux, this path is usually tmp, and under Windows, it is usually C:WindowsTemp. When there are multiple PHP applications on the server, they will save their session files in the same directory. Similarly, these PHP applications will also start GC at a certain probability and scan all session files.
The problem is that when GC is working, it does not distinguish between sessions on different sites. For example, site A's gc_maxlifetime is set to 2 hours, and site B's gc_maxlifetime is set to the default 24 minutes. When site B's GC starts, it will scan the public temporary file directory and delete all session files older than 24 minutes, regardless of whether they come from site A or B. In this way, the gc_maxlifetime setting of site A is useless.
Once you find the problem, it’s easy to solve it. Modify the session.save_path parameter, or use the session_save_path() function to point the directory where the session is saved to a dedicated directory. The gc_maxlifetime parameter works normally.
Strictly speaking, is this a bug in PHP?
Another problem is that gc_maxlifetime can only guarantee the shortest time for the session to survive, and cannot be saved. After this time, the session information will be deleted immediately.Because GC is started based on probability and may not be started for a long period of time, a large number of sessions will still be valid after exceeding gc_maxlifetime.
One way to solve this problem is to increase the probability of session.gc_probability/session.gc_divisor. If mentioned to 100%, this problem will be completely solved, but it will obviously have a serious impact on performance. Another method is to determine the lifetime of the current session in your code. If it exceeds gc_maxlifetime, clear the current session.
But if you do not have the permission to operate the server, it will be more troublesome. You need to rewrite the SessionID through the PHP program to achieve permanent session data storage. Check the function manual of php.net and you can see the "session_id" function: if no parameters are set, the current SessionID will be returned. If the parameters are set, the current SessionID will be set to the given value...
As long as you use a permanent cookie and add the "session_id" function, you can save permanent session data!
But for convenience, we need to know the "session.name" set by the server, but most users do not have permission to view the php.ini settings of the server. However, PHP provides a very good function "phpinfo", which can be used to view Almost all PHP information!
-------------------------------------------------- -------------------------------------