This article mainly introduces related information about php writing sessions to the database. For more information, see use the session_set_save_handler () function to write Session content to the database.
<? Php/** @ author Fahy * the database is mysql, * the database name is session, and the table name is session. * The fields in the table include PHPSESSID, update_time, client_ip, data */class Session {private static $ handler = null; private static $ ip = null; private static $ lifetime = null; private static $ time = null; // Configure the static variable private static function init ($ handler) {self: $ handler = $ handler; // Obtain the database resource self: $ ip =! Empty ($ _ SERVER ["REMOTE_ADDR"])? $ _ SERVER ["REMOTE_ADDR"]: 'unkonw'; // Obtain the client ip self: $ lifetime = ini_get ('session. gc_maxlifetime '); // Get the session life cycle self: $ time = time (); // get the current time} // call session_set_save_handler () function and enable session static function start ($ pdo) {self: init ($ pdo); session_set_save_handler (array (_ CLASS __, 'open '), array (_ CLASS __, 'close'), array (_ CLASS __, 'read'), array (_ CLASS __, 'write '), array (_ CLASS __, 'deststroy'), array (__ CLASS __, 'gc '); session_start ();} public static function open ($ path, $ name) {return true;} public static function close () {return true;} // query the database's public static function read ($ PHPSESSID) {$ 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) {return '';} if (self: $ ip = $ result ['Client _ IP']) {self: destroy ($ PHPSESSID); return '';} if ($ result ['update _ time'] + self: $ lifetime)
Prepare ($ SQL); $ stmt-> execute (array ($ PHPSESSID); if ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC )) {if ($ result ['data']! = $ Data | self: $ time> ($ result ['update _ time'] + 30) {$ SQL = "update session set update_time = ?, Data =? Where PHPSESSID =? "; $ Stmt = self: $ handler-> prepare ($ SQL); $ stmt-> execute (array ($ self: $ time, $ data, $ PHPSESSID) ;}} else {if (! Empty ($ data) {try {$ SQL = "insert into session (PHPSESSID, update_time, client_ip, data) values (?,?,?,?) ";} Catch (PDOException $ e) {echo $ e-> getMessage ();} $ th = self: $ handler-> prepare ($ SQL ); $ Something-> execute (array ($ PHPSESSID, self ::$ time, self ::$ ip, $ data) ;}} return true ;} public static function destroy ($ PHPSESSID) {$ SQL = "delete from session where PHPSESSID =? "; $ Stmt = self: $ handler-> prepare ($ SQL); $ stmt-> execute (array ($ PHPSESSID); return true ;} public static function gc ($ lifetime) {$ SQL = "delete from session where update_time <? "; $ Stmt = self: $ handler-> prepare ($ SQL); $ stmt-> execute (array (self: $ time-$ lifetime )); return true ;}} // use PDO to connect to the database try {$ pdo = new PDO ("mysql: host = localhost; dbname = session", "root ", "hwj193");} catch (PDOException $ e) {echo $ e-> getMessage ();} // Pass database resource Session: start ($ pdo );
The above is all the content of this article. I hope you will like it.