In PHP4.0, the support for Session is added to facilitate our many programs, such as shopping carts, and more.
In many forums, session is also used to process users' logins, record username and password, so that users don't have to enter their username and password every time! However, the general session life is limited. If the user closes the browser, it cannot save the SESSION variable! So how can I achieve the permanent life of the session?
Everyone knows that session is stored in the server side, obtain this user's file according to the sessionId provided by the client, and then reads the file, obtains the value of the variable, the sessionID can use the client's cookie or HTTP1.1 protocol Query_String (That is the URL of access) "?" The following part) to transfer to the server, then the server reads the session directory ...
To implement the permanent life of the session, you first need to understand the PHP.INI's related settings for the session (open the php.ini file, "[session]" section):
1, session.use_cookies: The default value is "1", representing the sessionID to pass using cookies, and then use query_string to pass;
2, session.name: This is the variable name stored by sessionID, which may be cookie, or query_string to pass, the default value is "phpsessid";
3, session.cookie_lifetime: This represents SessionID's time in the client cookie, the default is 0, representing the browser one closed sessionID is a waste ... is because this SESSION cannot be used forever!
4, session.gc_maxlifetime: This is the time of session data in server-side, if it exceeds this time, then session data is automatically deleted!
There are still many settings, but it is related to this article, and the principles and steps of permanent session began below.
As mentioned earlier, the server reads the session data by sessionid, but the sessionID transmitted by the general browser is not there after the browser is turned off, then we only need to manually set the sessionID and save it, no. ...
If you have a server's Cao as authority, then set this very simple, just need to perform the following steps:
1. Set "session.use_cookies" to 1, open the cookie to save the sessionID, but the default is 1, generally no modification;
2, change "session.cookie_lifetime" to be infinite (of course, there is no infinite parameter, but 999999999 and is endless);
3. Set the "session.gc_maxlifetime" set to "session.cookie_lifetime";
After the setting is completed, open the editor and enter the following code:
------------------------------------------
session_start ();
Session_register ('count');
$ count ;
Echo $ count;
?>
------------------------------------------ then saved as "session_check.php" Use the browser to open "session_check.php", see if it is "1", then close the browser, then open the browser to access "session_check.php", if "2" is displayed, then congratulations, you already Success; if you fail, check your front settings.
But if you don't have a server's Cao as authority, then it is more troublesome, you need to rewrite the sessionID through the PHP program to achieve permanent session data saving. Check the function manual of php.net, you can see "SESSION_ID" function: If no parameters are set, then the current sessionid, if the parameters are set, the current sessionID is set to the value given ...
Just use the permanent cookie plus the "session_id" function, you can save your permanent session data!
But for convenience, we need to know the "session.name" of the server settings, but the general user has no permission to view the server's php.ini settings, but PHP provides a very good function "PHPINFO", using this can see almost all PHP information!
----------------------------------
phpinfo ()?>
------------------------------------
Open the editor, enter the above code, and then run this program in the browser, see information about PHP. One of these "session.name" parameters, this is the server "session.name" we need, generally "phpsessid".
After you write down the name of the sessionID, we can achieve a permanent session data store!
Open the editor and enter the following code:
-------------------------------------------------- ---------------------------
session_start (); // Start session
Session_register ('count'); // Register Session Variable COUNT
IF (Isset ($ PHPSESSID)) {
Session_id ($ PHPSESSID);
} // If you set a $ pHPSESSID, you will assign the sessionID to $ PHPSESSID, otherwise generates sessionid
$ PHPSESSID = session_id (); // Get the current sessionID
$ count ; // Variable COUNT plus 1
SetCookie ('phpsessid', $ PHPSESSID, TIME () 3156000); // Save SessionID to Cookie
Echo $ count; // Display the value of session variable count
?>
-------------------------------------------------- ------------------------------
After saving, using the same method as the detection of server rights, detects whether the sessionID is successfully saved.
postscript:
In fact, the true permanent storage is impossible, because the Cookie has limited storage time, and the server's space is limited ... but for some sites that need to save time, the above method is already enough! Finally, the author's debugging environment: Windows98Digext (SE) Apache PHP 4.04.