Session Advanced App writes user information to the database
First create a database table
Set up session table in experimental database SQLDB for storing data
Create the required files in the root directory (focus on session,class.php This class file, including some methods of column)
The main use of session.class.php in the Session_set_save_handler () This method, the use of PDO for data manipulation, write to the database table with the class,
A static method is defined in the class, and its properties are static, so that the session data is written directly to the database, not in a local folder
First set up a session class, the class first defines some private static properties, defines the IP, time and time to live
<?php//defines Session class session{ private static $handler =null; private static $ip =null; private static $lifetime =null; private static $time =null;
private static function init ($handler) {self :: $handler = $handler;//link for PDO//IP first judge not empty self :: $ip =! Empty ($_server["REMOTE_ADDR"])? $_server["REMOTE_ADDR"]: ' unkown '; Remove the time to live from the profile self :: $lifetime =ini_get (' session.gc_maxlifetime '); Self:: $time =time (); }
The next step is to define how to open the session
Defines the method of opening session static function start (PDO $pdo) { self::init ($pdo); Initializes the private method Session_set_save_handler ( array (__class__, "open"), Array (__class__, "close"), Array (__class__, "read"), Array (__class__, "write"), Array (__class__, "destroy"), Array ( __class__, "GC") ); Session_Start ();}
In the open session there are open, close, read, write, Destory, GC methods, the following is mainly to define these methods
Open () and close () methods
public static function open ($path, $name) { return true;} public static function Close () { return true;}
When defining these methods, the most important is the write () and read () methods, as this is read directly from the database or written, using the PDO database preprocessing method
Read (): Advanced line PDO preprocessing, and then in the obtained record, to determine whether the IP is the IP in the database, whether the data taken out has expired, is not a successful read out
public static function read ($PHPSESSID) { $sql = ' Select Phpsessid,update_time,client_ip,data from session where Phpsessid=? "; With the parameter //pdo pretreatment $stmt =self:: $handler->prepare ($sql); $stmt->execute (Array ($PHPSESSID)); Gets a record if (! $result = $stmt->fetch (PDO::FETCH_ASSOC)) { return '; } Determines whether the current access IP is a database IP if (self:: $ip! = $result ["Client_ip"]) {self ::d Estroy ($PHPSESSID); Destroy user return '; } Determine if the if is expired ( ($result ["update_time"] + self:: $lifetime) < self:: $time) {self ::d Estroy ($PHPSESSID); return '; } return $result [' data ']; Successful read out }
Write (): The same method of writing is also important, you need to determine whether the data passed in is empty, if it is empty do not insert
public static function Write ($PHPSESSID, $data) { $sql = ' Select Phpsessid,update_time,client_ip,data from session where phpsessid=? "; $stmt =self:: $handler->prepare ($sql); $stmt->execute (Array ($PHPSESSID)); if ($result = $stmt->fetch (PDO::FETCH_ASSOC)) { //Delay 30 Update if ($result [' data ']! = $data | | Self:: $time > ($ result[' Update_time ']+30) { //Update data statement $sql = "Uptate session set update_time=?, Data=? where phpsessid=?"; $stm =self:: $handler->prepare ($sql); $stm->execute (Array (self:: $time, $data, $PHPSESSID)); } } else{ //Determine if the data passed in is empty, empty without inserting if (!empty ($data)) { $sql = "INSERT into session (Phpsessid,update_time, Client_ip,data) VALUES (?,?,?,?) "; Insert value with? parameter $sth =self:: $handler->prepare ($sql); $sth->execute (Array ($PHPSESSID, self:: $time, Self:: $ip, $data)); Must use array } } to return true;
The next step is data destruction.
Same destory () and GC ()
Destory (): Data deletion
GC (): Garbage collection
public static function Destroy ($PHPSESSID) { $sql = ' Delete from session where phpsessid=? '; $stmt =self:: $handler->prepare ($sql); $stmt->execute (Array ($PHPSESSID)); return true; } private static function GC ($lifetime) { $sql = "Delete from session where Update_time <?"; $stmt =self:: $handler->prepare ($sql); $stmt->execute (Array (self:: $time-$lifetime)); return true; } }
Finally, throw an exception and call the session class
try{ $pdo =new PDO ("Mysql:host=localhost;dbname=sqldb", "root", "heyifeng19930924"); } catch (Pdoexception $e) { echo $e->getmessage (); } Call Session class Session::start ($PDO);
In the test file, the notation is the same as the advanced usage of the session (the test file for the previous blog)
Just include this class file in the Include file
Namely: include "session.class.php";
Test results, if the insert data is successful, query the table information, displayed in the database:
That is, passing the value of the column Phpsessid
After deleting the undo, the query table displays
The value of PHPSESSID is revoked.
PHP writes user information to the database