Write sessions to the database, and write sessions to the database.
Use the session_set_save_handler () function to write Session content to the database.
1 <? Php 2/* 3 * @ author Fahy 4 * @ link http://home.cnblogs.com/u/HuangWj 5 * database for mysql, 6 * database name for session, table name for session, 7 * Table fields include PHPSESSID, update_time, client_ip, data 8 */9 class Session {10 private static $ handler = null; 11 private static $ ip = null; 12 private static $ lifetime = null; 13 private static $ time = null; 14 15 // configure the static variable 16 private static function init ($ handler) {17 self: $ handler = $ handler ;// Retrieve database resources 18 self ::$ ip =! Empty ($ _ SERVER ["REMOTE_ADDR"])? $ _ SERVER ["REMOTE_ADDR"]: 'unkonw'; // get Client ip 19 self: $ lifetime = ini_get ('session. gc_maxlifetime '); // get session lifecycle 20 self: $ time = time (); // get current time 21} 22 // call session_set_save_handler () function and enable session 23 static function start ($ pdo) {24 self: init ($ pdo); 25 session_set_save_handler (26 array (_ CLASS __, 'open '), 27 array (_ CLASS __, 'close'), 28 array (_ CLASS __, 'read'), 29 array (_ CLASS __, 'write'), 30 Array (_ CLASS __, 'deststroy'), 31 array (_ CLASS __, 'gc ') 32); 33 session_start (); 34} 35 36 public static function open ($ path, $ name) {37 return true; 38} 39 public static function close () {40 return true; 41} 42 43 // query database data 44 public static function read ($ PHPSESSID) {45 $ SQL = "select PHPSESSID, update_time, client_ip, data from session where PHPSESSID =? "; 46 $ stmt = self: $ handler-> prepare ($ SQL); 47 $ stmt-> execute (array ($ PHPSESSID); 48 if (! $ Result = $ stmt-> fetch (PDO: FETCH_ASSOC) {49 return ''; 50} 51 if (self :: $ ip = $ result ['client _ ip']) {52 self: destroy ($ PHPSESSID); 53 return ''; 54} 55 if ($ result ['Update _ time'] + self: $ lifetime) <self: $ time) {56 self: destroy ($ PHPSESSID ); 57 return ''; 58} 59 return $ result ['data']; 60} 61/* 62 * First, check whether the session has data. If yes, update the data, if the data does not exist, insert 63 */64 // write the session into the database. $ data is transferred to the keys and values in the session. Group 65 public static function write ($ PHPSESSID, $ data) {66 $ SQL = "select PHPSESSID, update_time, client_ip, data from session where PHPSESSID =? "; 67 $ stmt = self: $ handler-> prepare ($ SQL); 68 $ stmt-> execute (array ($ PHPSESSID )); 69 70 if ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC) {71 if ($ result ['data']! = $ Data | self: $ time> ($ result ['Update _ time'] + 30) {72 $ SQL = "update session set update_time = ?, Data =? Where PHPSESSID =? "; 73 $ stmt = self: $ handler-> prepare ($ SQL); 74 $ stmt-> execute (array ($ self: $ time, $ data, $ PHPSESSID); 75} 76} else {77 if (! Empty ($ data) {78 try {79 $ SQL = "insert into session (PHPSESSID, update_time, client_ip, data) values (?,?,?,?) "; 80} catch (PDOException $ e) {81 echo $ e-> getMessage (); 82} 83 $ th = self: $ handler-> prepare ($ SQL ); 84 $ something-> execute (array ($ PHPSESSID, self: $ time, self: $ ip, $ data); 85} 86} 87 return true; 88} 89 90 public static function destroy ($ PHPSESSID) {91 $ SQL = "delete from session where PHPSESSID =? "; 92 $ stmt = self: $ handler-> prepare ($ SQL); 93 $ stmt-> execute (array ($ PHPSESSID); 94 return true; 95} 96 public static function gc ($ lifetime) {97 $ SQL = "delete from session where update_time <? "; 98 $ stmt = self: $ handler-> prepare ($ SQL); 99 $ stmt-> execute (array (self: $ time-$ lifetime )); 100 return true; 101} 102} 103 // use PDO to connect to the database 104 try {105 $ pdo = new PDO ("mysql: host = localhost; dbname = session ", "root", "hwj193"); 106} catch (PDOException $ e) {107 echo $ e-> getMessage (); 108} 109 // pass database resource 110 Session :: start ($ pdo );