The method of using infinite lifetime session

Source: Internet
Author: User
Keywords The method of using infinite lifetime session
In the php4.0 added to the session support, convenient for many of our programs, such as shopping cart and so on!
In many forums, the session is also used to process the user login, record the user name and password, so that users do not have to enter their own user name and password every time! However, the life of the general session is limited, if the user closed the browser, you can not save the session variables! So how can we achieve the permanent life of the session?
As you know, the session is stored on the server side, according to the client-provided SessionID to get the user's files, and then read the file, get the value of the variable, SessionID can use the client's cookie or Http1.1 protocol Query_ String (the "?" of the URL that is visited Later) to the server, and then the server reads the Session Directory ...

To achieve the permanent lifetime of the session, you first need to know about the php.ini settings for the session (open the PHP.ini file, in the [Session] section):
1, Session.use_cookies: The default value is "1", on behalf of SessionID using cookies to pass, the other is the use of query_string to pass;
2, Session.name: This is sessionid stored variable name, may be a cookie, it may be query_string to pass, the default value is "PHPSESSID";
3, Session.cookie_lifetime: This represents SessionID in the client cookie storage time, the default is 0, on behalf of the browser a close SessionID is void ... This is why the session cannot be used permanently!
4, Session.gc_maxlifetime: This is the session data in the server-side storage time, if more than this time, then the session data will be automatically deleted!
There are a lot of settings, but that's what this article is about, and here's how to use the permanent session principle and procedure.

As mentioned earlier, the server through the SessionID to read the session data, but the General browser transfer of SessionID after the browser is closed, then we only need to set SessionID and save the human, not can ...
If you have permission to operate the server, it is very, very simple to set up, just the following steps:
1, the "Session.use_cookies" set to 1, open the cookie storage SessionID, but the default is 1, generally do not change;
2, the "Session.cookie_lifetime" to the positive infinity (of course, no positive infinity parameters, but 999999999 and positive infinity is no difference);
3, the "Session.gc_maxlifetime" set to "Session.cookie_lifetime" the same time;
Once setup is complete, open the editor and enter the following code:
------------------------------------------------------------------------------------
Session_Start ();
Session_register (' count ');
$count + +;
Echo $count;
?>
------------------------------------------------------------------------------------
Then save as "session_check.php", use the browser to open "session_check.php", to see if the display is "1", then close the browser, and then open the browser to access "session_check.php", if the display "2", So congratulations, you have succeeded; If you fail, please check your previous settings.

But if you do not have the server operation permissions, it is more troublesome, you need to rewrite the PHP program SessionID to achieve permanent session data preservation. Check the Php.net function manual, you can see the "session_id" this function: if the parameter is not set, then the current SessionID will be returned, if the parameter is set, the current SessionID is set to the given value ...
As long as the permanent cookie with the "session_id" function, you can achieve the permanent session data saved!
But for convenience, we need to know the server settings "Session.name", but the general user does not have permission to view the server's php.ini settings, but PHP provides a very good function "phpinfo", use this can see almost all the PHP information!
------------------------------------------------------------------------------------
<title>PHP Related Information display</title>

------------------------------------------------------------------------------------
Open the editor, enter the code above, and run the program in the browser, and see information about PHP (1). There is a "session.name" parameter (shown in the figure), this is the server we need "Session.name", is generally "PHPSESSID".
After we have written down the name of the SessionID, we can achieve a permanent session data storage!
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 $PHPSESSID is set, the SessionID is assigned to $PHPSESSID, otherwise the build SessionID
$PHPSESSID = session_id (); Get the current SessionID
$count + +; Variable count plus 1
Setcookie (' Phpsessid ', $PHPSESSID, Time () +3156000); Store SessionID into cookies
Echo $count; Displays the value of the session variable count
?>
------------------------------------------------------------------------------------

After saving, the use of the same time as the server has just the same detection method, detection of the success of the saved SessionID.


Postscript:
In fact, the real permanent storage is impossible, because the cookie storage time is limited, and the server space is limited ... But for some sites that need to be kept longer, the above method is enough! For other applications of the session, refer to the zphp.com article.
Finally, the author's debugging Environment: Windows98digext (SE) +apache+php 4.04. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.