1. What is a session
We discussed in the previous section that there are many drawbacks to storing data in the form of cookies on the client. In order to solve these problems, there is a session.
Session is the data stored on the server side in the form of a file.
By sending a cookie to the client to save the SessionID, the server establishes the session with the client.
Browser: Cookies (SessionID) <------one by one corresponding------> (SessionID) Session: Server
2. Open a session
Session_Start (); /* open a session and do two things, basically . */ /* 1 If the client has a SessionID saved with a cookie, then the corresponding session data is taken into memory */ /* 2 if not, send a new sessionid in the form of a cookie to the client to save */
Note: If it is a cookie-based session because the cookie is passed in the HTTP header, Session_star () and Setcookie () cannot have any output before this.
, run once Session_Start () and found a cookie named Phpsessid in the browser.
We use the following code to print out the client's SessionID
Session_Start (); Echo session_id (); // LDLQ3N79JHKBDT120IACQ19VS6
The IDs of the two are consistent. If you do not close the browser (because the default is to delete Phpsessid when the browser is closed), opening a new page will still print the same sessionid.
If you want to change the time that you save the SessionID cookie, you can modify the Session.cookie_lifetime option (in seconds) in the configuration file, which defaults to 0, which means that the browser is deleted when it is closed.
3.Session Access Data
PHP uses a global array $_session to access data, which is used just like a normal array.
It is worth mentioning that in-memory data is destroyed after the script is executed, so the data in the session is actually stored as a file on the hard disk. When the session is opened, the saved data is read into memory for ease of use.
These operations are handled automatically by PHP.
As below, create a session data
Session_Start (); Echo session_id (); $_session [' Test ']= "This is a session";
When the code executes, a file is created to hold the data, and the file name is the SessionID of the session.
This directory is configured by Session.save_path in the configuration file and is generally './tmp '.
My directory is "e:xampp/tmp", open Directory to find files
Open the file and look.
This is the value that was previously saved.
4. End a session
/*The first step, open the session, if not open the session PHP to get SessionID, how does php know to delete the session?*/ Session_Start(); /*in the second step, the data in the session has been read into memory, so you have to empty it .*/ /*Note that you cannot use unset ($_session), because we just want to clear the data instead of deleting this global array*/ $_session=Array(); /*Step Three, delete the client's cookie*/ /*Note: The default cookie name is PHPSESSID but this name can be changed by Session.name in the configuration file.*/ /*so it's safer to use the PHPSESSID or the Session_name () function to get the cookie name .*/ if(isset($_cookie[Session_name()])) { Setcookie(Session_name(),‘‘, Time()-1, '/'); } /*Fourth Step, delete the server-side files*/ Session_destroy();
5. Garbage cleanup mechanism
If the cookie life cycle of the browser is deleted, then we have no way to delete the session file saved on the hard disk with the above method. The next time the client access is assigned to a new SessionID, the original file is not available.
So how do you delete these junk files?
PHP has a garbage cleanup mechanism.
There are three important options in the configuration file
Session.gc_maxlifetime= specifies a time, in seconds, when the garbage cleanup mechanism is started if the session file's modification time is greater than the value set by this property, the garbage cleanup mechanism will delete the file, or vice versa.
Session.gc_probability and Session.gc_divisor, set S=session.gc_probability/session.gc_divisor, then each session_start () The chance to start a garbage cleanup mechanism when you have S.
s value too larger than small are not good, general Session.gc_probability=1, and Session.gc_divisor= (1000~5000).
php-Session Control-session