Php permanent session principle:
1. enable cookie in the browser. If PHP calls session_start ();
When you access the page for the first time (there is no session information in the request), the server will generate a session id to the "response header information" (only returned for the first time)
Php code
Set-Cookie PHPSESSID = a2e6b7db91b89014344c254c1db0c086; path =/
The browser sets the session id in the cookie named PHPSESSID. each time you access the page, the browser will include the session id in the "request header information ".
Php code
Cookie PHPSESSID = a2e6b7db91b89014344c254c1db0c086
2. when the browser disables cookies, the session id must be passed through the url.
SESSIONID can be passed in each url
Manual setting at the beginning of the PHP page
Php code
If (isset ($ _ GET ['sessionid']) {session_id ($ _ GET ['sessionid']);} session_start ();
3. session_start (); meaning:
If session_id ($ sid) is called before, $ sid is used as the session id.
If no call is made, use $ _ COOKIE [session name] as the session id.
The session name can be set using session_name ($ sname) before the call (PHPSESSID by default)
A known method is to use session_set_save_handler to take over all session management tasks. generally, session information is stored in the database, so that all expired sessions can be deleted through SQL statements, precisely control the validity period of the session. This is also a common PHP-based method for large websites. However, small websites do not seem to have to work hard.
However, the life cycle of a general Session is limited. if the user closes the browser, the Session variable cannot be saved! So how can we achieve the permanent life of the Session?
As you know, sessions are stored on the server. you can obtain the user's file based on the SessionID provided by the client, read the file, and obtain the variable value, sessionID can use the Cookie of the client or Query_String of the Http1.1 protocol (that is, the "?" To the server, and then the server reads the Session directory ......
To realize the permanent life of a Session, first you need to know about the Session settings of php. ini (open the php. ini file, in the "[Session]" section ):
1. session. use_cookies: The default value is "1", which indicates that SessionID is transmitted using cookies, and Query_String is used for transmission;
2. session. name: the name of the variable stored by SessionID, which may be Cookie or Query_String. the default value is "PHPSESSID ";
3. session. cookie_lifetime: This indicates the time when SessionID is stored in the Cookie on the client. the default value is 0, indicating that the session id will be voided as soon as the browser closes the session id ...... This is why the Session cannot be used permanently!
4. session. gc_maxlifetime: the time when Session data is stored on the server. if this time is exceeded, the Session data is automatically deleted!
There are still a lot of settings, but this is related to this article. The following describes the principles and steps for using permanent sessions.
As mentioned above, the server reads Session data through SessionID, but generally the SessionID sent by the browser does not exist after the browser is closed. Therefore, we only need to manually set the SessionID and save it, no ......
If you have operation permissions on the server, it is very easy to set up, but you only need to perform the following steps:
1. set "session. use_cookies" to 1. enable the Cookie to store the SessionID. However, the default value is 1, which generally does not need to be modified;
2. change "session. cookie_lifetime" to positive infinity (of course there are no positive infinity parameters, but there is no difference between 999999999 and positive infinity );
3. set "session. gc_maxlifetime" to the same time as "session. cookie_lifetime;
The PHP document clearly states that the parameter for setting the session validity period is session. gc_maxlifetime. You can modify this parameter in the php. ini file or through the ini_set () function. The problem is that after multiple tests, modifying this parameter does not work, and the session validity period remains the default value for 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 use the global variable session. gc_probability/session. gc_pisor (you can also use php. ini or ini_set () function to modify) to determine whether to start a GC (Garbage Collector ). By default, session. gc_probability = 1, session. gc_pisor = 100, that is, there is a 1% possibility to start GC.
GC is used to scan all session information and subtract the last modification time (modified date) of the session from the current time. compare the gc_maxlifetime parameter. if the survival time exceeds gc_maxlifetime, delete the session.
So far, everything works normally. Why is gc_maxlifetime invalid?
By default, session information is saved as a text file in the temporary file directory of the system. In Linux, this path is usually \ tmp, and in Windows it is usually C: \ Windows \ Temp. When there are multiple PHP applications on the server, they will save their session files in the same directory. Similarly, these PHP applications start GC and scan all session files at a certain rate.
The problem is that GC does not differentiate sessions of different sites during work. For example, the gc_maxlifetime of site A is set to 2 hours, and the gc_maxlifetime of Site B is set to the default 24 minutes. When GC of Site B is started, it will scan the public temporary file directory and delete all session files that have exceeded 24 minutes, regardless of whether they are from site A or site B. In this way, the gc_maxlifetime setting of site A is virtually empty.
Locate the problem and solve it easily. Modify the session. save_path parameter, or use the session_save_path () function to direct the Directory of the session to a dedicated directory. the gc_maxlifetime parameter works properly.
Strictly speaking, is this a PHP bug?
Another problem is that gc_maxlifetime can only ensure the shortest time for session survival. it cannot be saved until this time expires and the session information will be deleted immediately. GC is started by chance and may not be started for a long time. Therefore, a large number of sessions will still be valid after gc_maxlifetime is exceeded. One way to solve this problem is to set the session. gc_probability/session. the probability of gc_pisor increases. if 100% is mentioned, this problem will be completely solved, but it will obviously have a serious impact on performance. Another method is to determine the survival time of the current session in the code. if the time exceeds gc_maxlifetime, the current session is cleared.
However, if you do not have the server operation permission, it is quite troublesome. you need to rewrite the SessionID through the PHP program to save Session data permanently. Check the php.net function manual and you can see the "session_id" function: If no parameter is set, the current SessionID is returned. if a parameter is set, the current SessionID will be set to the given value ......
You can use the permanent Cookie and the "session_id" function to save the permanent Session data!
However, for convenience, we need to know the "session. name ", but generally users do not have the permission to view the server's php. ini settings, but PHP provides a very good function "phpinfo", which can be used to view almost all PHP information!
Bytes ------------------------------------------------------------------------------------
PHP Information Display
Bytes ------------------------------------------------------------------------------------
Open the editor, enter the above code, and then run the program in the browser. The PHP information is displayed, as shown in 1 ). There is a "session. name" parameter. this is the server "session. name" we need, which is generally "PHPSESSID ".
After recording the SessionID name, we can store Session data permanently!
The code is as follows:
Session_start ();
Ini_set ('session. save_path ','/tmp /');
// 6 hours
Ini_set ('session. gc_maxlifetime', 21600 );
// Save for one day
$ LifeTime = 24*3600;
Setcookie (session_name (), session_id (), time () + $ lifeTime ,"/");
Postscript:
In fact, the real permanent storage is impossible, because the Cookie storage time is limited, and the server space is limited ...... However, the above method is sufficient for some sites that need to be stored for a long time!
By default, the php session is saved as a file, which can be used only on windows with a low File space overhead, however, if we use a file system on the uinx or liux, the file space overhead of such a file system is very large, but the session must be used all the time, A large number of users create a lot of session files, which brings performance problems to the entire server. on the other hand, if the server starts to use the cluster method, the session consistency cannot be maintained, so we are ready to use the database to save the session. no matter how many servers are used at the same time, as long as their sessions are saved on a database server, they can save the complete session. continue with the implementation of the session.
By default, php sessions are stored in the file format. you can see this line in ini, session. save_handler = "files", which means to use files to save sessions. to use databases to store sessions, we need to change it to the support mode and rename it session. save_handler = "use". However, this only means that we have not stored the session in the file mode. we also need to select a database and create a database table.
To create a table structure for databases and databases, we can use any database that php can use. because php and mysql have the best combination, I will use mysql as an example, of course, you can change the name of another database based on your needs. at the same time, because mysql has no transaction function, it is faster than other databases. However, you can save the session book and never process things, in addition, I decided better.
Create database 'session '; create table 'session' (id CHAR (30) not null, 'user' CHAR (30), data CHAR (3000 ), parmiry by ('id '));
Compile php files
Php code
Save as session_user_start.php.
Now our work has been completed, as long as you need to use session, set session_user_start.php. Include in. Note that this file must be included in the first line of the file, and you can use the same method as the session of the file.
Sessions of a.te.com and B .te.com are separated.
Www.te.com does not receive session from a.te.com.
However, a.te.com does not receive a session from www.te.com.
Set the session data sharing method for a.phptest.com and B .phptest.com, and set the same sessionid
Php code
3600, 'path' => '/', 'domain '=>' .phptest.com ', 'Secure' => false, 'httponly '=> true ,); call_user_func_array ('session _ set_cookie_params ', $ cookieParams); session_start (); var_dump (session_id ());