Method using an infinite life period session
Author: Unknown Updated: Legume Source: ddvip.net Update Time: 2004.08.22 Contributor mail: tg * ddvip.com this article would not say very good, but I believe most PHPer have thought about these issues. The use of the infinite life period session joined the support for Session in PHP4.0, which is convenient for us to many programs, such as shopping carts, etc.! 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) "?" Later partial) to transfer to the server, then the server read the session directory ... To implement the permanent life of the session, first you 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 sessionID to pass using cookies, and then use query_string to pass; 2, session.name: This is the variable name stored by sessionID, It may be cookie, or 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 ... 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 through sessionid, but the sessionID transmitted by the general browser is not there after the browser is closed, then we only need to set the sessionID and save it, no, you can have ... If you have The server's operational permissions, then set this very simple, just need to perform the following steps: Session.cookie_lifetime, "Of course, there is no infinite parameter, but 99999999 and the infinite differences); 3. Set the" session.gc_maxlifetime "setting as the" session.cookie_lifetime "; after setting, Open the editor, enter the following code: Php session_start (); session_register ('count'); $ count ; echo $ count;?> And save it to "session_check.php", open "session_check.php" with your browser 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 have succeeded; if you fail, please check your front setting. But if you don't have the server's operational permissions, 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!