Php writes sessions to the database

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.