PHP method using an infinite life period session

zhaozj2021-02-16  94

PHP method using an infinite life period session

This post is reprinted from: http://blog.9cbs.net/whzhaha/archive/2004/07/30/56683.aspx

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 handle user logins, record usernames and passwords, 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 section) 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 a lot of settings, but this is related to this article, and the principles and steps of permanent session began below. ? I have said before, the server reads the session data through sessionid, but the sessionID transmitted by the general browser is not there after the browser is shut down, then we only need to set the sessionID and save it, not ...

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 need to modify ;? 2, change "session.cookie_lifetime" to be endless (of course, there is no infinite parameter, but 99999999 and the endless difference);? 3, set "session.gc_maxlifetime" setting as "session.cookie_lifetime" time;

After the setting is completed, open the editor and enter the following code:

----------------------------------------?

? session_start ();? session_register ('count'); $ count ;? echo $ count ;??>? ------------------------ ------------------ then save it to "session_check.php", open "session_check.php" with your browser to see if it is "1", then close the browse Open the browser to access "session_check.php", if "2" is displayed, then congratulations, you have succeeded; if you fail, please 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 you do not set a parameter, then the current sessionID, if the parameter is 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!

---------------------------------? PHP related information display?

? ------------------------------------

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, enter the following code:? --------------------------------------- -------------------------------------?

? session_start (); // Start session? session_register ('count'); // Register Session Variable Count? if (isset ($ PHPSESSID)) {? session_id ($ PHPSESSID) ;?} // If you set $ PHPSESSID, Just assign the sessionID to $ PHPSESSID, otherwise generate sessionid? $ PHPSESSID = session_id (); // get the current sessionid? $ Count ; // Variable COUNT plus 1? Setcookie ('phpsessid', $ phpsessid, time () 3156000 ); // Store sessionid to cookie? Echo $ count; // Display the value of the 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, true permanent storage is impossible, because 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.

转载请注明原文地址:https://www.9cbs.com/read-11760.html

New Post(0)