We know that session is a conversational technology that enables you to share data across scripts or to detect user status.
How the session works (1) When a session is first enabled, a unique identifier is stored in a local cookie. (2) First, using the Session_Start () function, PHP loads the stored session variables from the session repository. (3) When executing a PHP script, register the session variable by using the Session_register () function. (4) When the PHP script executes, the non-destroyed session variable is automatically saved in the session library under the local path, which can be specified by the Session.save_path in the php.ini file and can be loaded the next time the page is browsed.
The session is stored on the server side of the file, so the session may be due to too many files, will be in the query session file and read the time to create pressure. Generally, we have three kinds of solutions.
1. Using file layering (disadvantage: I/O operations are a bottleneck for the system, even if the hierarchy does not prevent this problem)
2. Put the session into the database
3. Place session in memory (non-relational database) (disadvantage: Teach high on server memory requirements)
With the increase of the session, management has been inconvenient.
Therefore, we choose a compromise approach, the session into the MySQL database, that is, we want to talk about the focus.
Set up a table management session.
Change the session's storage mechanism so that the session no longer exists in the file, but into the library.
More of the storage mechanism, just add the function Session_set_save_handler () to the file.
The code is as follows:
<?php ini_set ("Session.save_handler", "user"); Session.gc_probability = 1 Molecular ini_set ("session.gc_probability", 1); Session.gc_divisor = 1000 Denominator ini_set ("Session.gc_divisor", 2); Session.gc_maxlifetime = 1440 garbage collection time, session validity Session_set_save_handler ("open", "close", "read", "write", "destroy" , "GC"); Connect database function open () {@ $link = mysql_connect (' 127.0.0.1 ', ' root ', ' root '); mysql_query (' Set names UTF8 '); mysql_query (' use Wangbin '); The <span>open callback function is similar to the constructor of a class, is called when the session is opened. This is the first callback function that is invoked automatically after the session is started or by calling session_start () manually starting a session. This callback function successfully returns TRUE and returns false instead. </span>} function Close () {mysql_close (); The <span>close callback function resembles a destructor for a class. called after the write callback function call. The close callback function is also called after the session_write_close () function is called. This callback function successfully returns TRUE and returns false instead. </span> function Read ($sess _id) {$sql = "Select Session_data from ' Session ' where session_id = ' $sess _id ' "; $result = mysql_query ($sql); if ($rows = Mysql_fetch_assoc ($result)) {return $rows [' Session_data '];} else{return '; }<ol class= "Dp-py" start= "1" ><li class= "alt" ><span> if there is data in the session the,read callback function must return the string after the session data is encoded (serialized). </span></li><li class= "alt" ><span> if there is no data in the session,read the callback function returns an empty string. </span></li><li class= "alt" ><span> start session automatically or by calling session_start () After the function starts the session manually, </span></li><li class= the "alt" ><span>PHP internal call read callback function to get the session data. Before calling read , PHP calls the open callback function. </span></li><li class= "alt" ><span>read callback returns the string format after serialization must be associated with the write The callback function holds the data in exactly the same format. </span></li><li class= "alt" ><span>PHP automatically deserializes the returned string and populates the $_SESSION Super global variable. </span></li><li class= "alt" ><span> although the data looks similar to the serialize () function, But it needs to be reminded thatThey are different. </span></li><li class= "alt" ><span> please refer to: session.serialize_handler. </SPAN></LI></OL>&NBSP} function Write ($sess _id, $sess _data) {$sql = "INSERT INTO ' session ' (s Ession_id,session_data,session_time) VALUES (' $sess _id ', ' $sess _data ', Now ()) on duplicate key update session_data = ' $ Sess_data ', Session_time = Now () "; This is for GC () return mysql_query ($sql); /* <span> calls the write callback function when the session saves data. This callback function receives the current session ID and the string after the data serialization in $_session as a parameter. The process of serializing session data is done by PHP according to the session.serialize_handler SetPoint. </span> <span> The serialized data will be saved together with the session ID Association. When the call read callback function gets the data, the data returned must be fully consistent with the data passed into the write callback function. </span><span> PHP calls this callback function after the script finishes or calls the session_write_close () function. Note that the close callback function is called internally by,php after this callback function is called. </span> Note: <span>PHP calls write callback functions after the output stream has been written and closed , so the write callback functionThe debug information in is not exported to the browser. If you need to use debug output in the write callback function, It is recommended that you write the debug output to a file. </span> */ } function Destroy ($sess _id) {echo __function__; $sql = "Delete from ' session ' where session_id = ' $sess _id '"; return mysql_query ($sql); /* <span> when calling session_destroy () function, or calling session_regenerate_id () function and setting destroy This callback function is called when the parameter is TRUE . This callback function successfully returns &NBSP;TRUE, and vice versa returns &NBSP;FALSE. </span> */} function GC ($sess _id) {$maxlifetime = Ini_set ("Session.gc_maxlifetime"); Echo __function__; $sql = "Delete from ' Session ' where now ()-session_time > ' $maxlifetime '"; return mysql_query ($sql); /* <span> to clean up the old data in the session,php will call the garbage collection callback function from time to times. The call cycle is controlled by the session.gc_probability and session.gc_divisor parameters. The lifetime parameter passed into this callback function is set by session.gc_maxlifetime . This callback function successfully returns &NBSP;TRUE, and vice versa returns &NBSP;FALSE. </span> *} header ("Content-type: Text/html;charset=utf8 "); Session_Start (); $_session[' name ']= ' AA '; Echo session_id (); echo $_session[' name '];
summarize session running mechanism:1. When the session is opened, the syntax executes the function session_start (), and the PHP session mechanism reads the cookie on the browser side, which is syntactically denoted as $_cookie[' Phpsessid '. 2. Locate the session data stored on the server based on the cookie. 3. Deserialize the session data and assign a value to the variable $_session. 4. The operation of the variable $_session is then done on the variable, and the SESSION file is not updated. 5. Whether the Session_destroy () function is executed, and if so, delete the server-side session file. 6. At the end of the script, determine if there is a Sessin file, or whether the Session_destroy () method has been executed. If not executed, the data in the $_session variable is written to the SESSION file. If executed, then nothing is done.
Session how to join MySQL Library