: This article mainly introduces writing sessions to the database. For more information about PHP tutorials, see. Use the session_set_save_handler () function to write Session content to the database.
1
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)
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
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 );
The above section describes how to write sessions to the database, including related content, and hope to help those who are interested in the PHP Tutorial.