Methods for using an infinite lifetime session

Source: Internet
Author: User
Tags count ini phpinfo variable client
Session in PHP4.0 added to the session support, convenient for us a lot of programs, such as shopping carts and so on!
In many forums, session is also used to deal with user login, record user name and password, so that users do not have to enter their own username and password every time! But the life of the general session is limited, if the user closed the browser, you can not save the variable session! So how can you achieve the permanent life of the session?
As you know, the session is stored on the server side, according to the SessionID provided by the client to get the user's file, and then read the file, get the value of the variable, SessionID can use the client's cookie or Http1.1 protocol Query_ String (that is, the "?" of the URL that is accessed) Later) to the server, and then the server reads the directory of the session ...

To achieve the permanent lifetime of the session, you first need to know about the php.ini settings (open php.ini file, in the [Session] section):
1, Session.use_cookies: The default value is "1", on behalf of SessionID use cookies to pass, the other is the use of query_string to pass;
2, Session.name: This is sessionid stored variable name, may be cookies, may also 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 on the void ... This is why the session cannot be used permanently!
4, Session.gc_maxlifetime: This is the session data stored on the server side of the time, if more than this time, then the session data will be automatically deleted!
There are a lot of settings, but this is related to this article, the following is the principle and procedure of using permanent session.

Previously said, the server through the SessionID to read the session data, but the General browser transfer SessionID after the browser is closed, then we only need to set up the SessionID and save, can not ...
If you have the right to operate the server, setting this is very, very simple, just the following steps are required:
1, the "Session.use_cookies" set to 1, open the cookie store SessionID, but the default is 1, generally do not have to modify;
2, the "session.cookie_lifetime" Change to positive infinity (of course, there is no positive infinite parameters, but 999999999 and positive infinity is no difference);
3, the "Session.gc_maxlifetime" set to and "Session.cookie_lifetime" the same time;
When you are finished setting up, open the editor and enter the following code:
------------------------------------------------------------------------------------
?
Session_Start ();
Session_register (' count ');
$count + +;
Echo $count;
?>
------------------------------------------------------------------------------------
Then save as "session_check.php", with the browser to open "session_check.php" to see if the display is not "1", and then close the browser, and then open the browser to access the "session_check.php", if the display "2", So congratulations, you've succeeded, and if you fail, please check your previous settings.

But if you do not have the right to operate the server, it is more cumbersome, you need to rewrite the SessionID through the PHP program to achieve permanent session data preservation. Check php.net's function manual, you can see the "session_id" function: If no parameters are set, then the current SessionID will be returned, if the parameters are set, the current SessionID will be set to the given value ...
As long as the use of permanent cookies plus the "session_id" function, you can achieve permanent session data saved!
But for convenience, we need to know the server settings "Session.name", but the general user does not have the right to view the server's php.ini settings, but PHP provides a very good function "phpinfo", use this to view almost all of the PHP information!
------------------------------------------------------------------------------------
<title>php Related Information Display </title>
<?phpinfo ()?>
------------------------------------------------------------------------------------
Open the editor, enter the code above, and then run the program in the browser to see the relevant information about PHP (as shown in Figure 1). One of the "Session.name" parameters (already marked in the figure), this is the server we need "session.name", generally "PHPSESSID".
After you have noted the name of the SessionID, we will be able to implement 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 $PHPSESSID is set, assign SessionID to $phpsessid or generate 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, using the same method as the one that just had the server permissions, detects whether the SessionID was successfully saved.


Postscript:
In fact, the real permanent storage is not possible, because the cookie storage time is limited, and the server space is limited ... But for some need to save time longer site, the above method is enough! For other applications of the session, see Zphp.com's 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.