Home >Backend Development >PHP Tutorial >i hate myself for loving you How to use unlimited lifetime Session in php
Support for Session has been added to PHP4.0, which facilitates many of our programs, such as shopping carts, etc.!
In many forums, Session is also used to process user logins and record usernames and passwords, so that users do not have to enter their usernames and passwords every time! However, the life span of a general Session is limited. If the user closes the browser, the Session variables cannot be saved! So how can we achieve the permanent life span of Session?
As we all know, the Session is stored on the server side. The user's file is obtained based on the SessionID provided by the client, and then the file is read to obtain the value of the variable. The SessionID can use the client's Cookie or the Query_String of the Http1.1 protocol (which is accessed (the part after the "?" in the URL) is transmitted to the server, and then the server reads the directory of the Session...
To realize the permanent life of the Session, you first need to understand the relevant settings of the Session in php.ini (open the php.ini file , in the "[Session]" section):
1. session.use_cookies: The default value is "1", which means that SessionID is passed by Cookie, otherwise it is passed by Query_String;
2. session.name: This is the SessionID storage The variable name may be Cookie or Query_String to pass. The default value is "PHPSESSID";
3. session.cookie_lifetime: This represents the time the SessionID is stored in the client cookie. The default is 0, which means that the browser closes the SessionID. It's invalid... It's because of this that the Session cannot be used permanently!
4. session.gc_maxlifetime: This is the time that Session data is stored on the server side. If this time is exceeded, the Session data will be automatically deleted!
There are many more settings, but these are the ones related to this article. Let’s start with the principles and steps of using permanent Session.
As mentioned before, the server reads Session data through SessionID, but generally the SessionID sent by the browser is gone after the browser is closed, then we only need to manually set the SessionID and save it, otherwise it will be ok...
If You have the operating authority of the server, so setting this is very, very simple. You just need to perform the following steps:
1. Set "session.use_cookies" to 1 and turn on Cookie to store SessionID, but the default is 1, generally no need to modify;
2. Change "session.cookie_lifetime" to positive infinity (of course there is no parameter for positive infinity, but there is no difference between 999999999 and positive infinity);
3. Set "session.gc_maxlifetime" to the same time as "session.cookie_lifetime" ;
After setting up, open the editor and enter the following code:
---------------------------------- --------------------------------------------------
session_start();
session_register("count");
$count++;
echo $count;
?>
-------------------------------- -------------------------------------------------- ---------------
Then save it as "session_check.php", open "session_check.php" with a browser, see if "1" is displayed, and then close the browser. Then open the browser and access "session_check.php". If "2" is displayed, congratulations, you have succeeded; if it fails, please check your previous settings.
But if you do not have the permission to operate the server, it will be more troublesome. You need to rewrite the SessionID through the PHP program to achieve permanent session data storage. Check the function manual of php.net and you can see the "session_id" function: if no parameters are set, the current SessionID will be returned. If the parameters are set, the current SessionID will be set to the given value...
As long as you use a permanent cookie and add the "session_id" function, you can save permanent session data!
But for convenience, we need to know the "session.name" of the server settings, but most users do not have permission to view the server's php.ini settings. However, PHP provides a very good function "phpinfo", which can be used to view almost all settings. PHP information!
------------------------------------------------- ----------------------------------
The above introduces the method of using infinite lifetime Session under i hate myself for loving you php, including the content of i hate myself for loving you. I hope it will be helpful to friends who are interested in PHP tutorials.