Session saved to database

Source: Internet
Author: User
? PhpclassSessionToDB {private $ _ path = null; private $ _ name = null; private $ _ pdo = null; private $ _ ip = null; private $ _ maxLifeTime = 0; publicfunction _ constr

 

 
 
  1. class SessionToDB 
  2.     private $_path         = null; 
  3.     private $_name         = null; 
  4.     private $_pdo          = null; 
  5.     private $_ip           = null; 
  6.     private $_maxLifeTime  = 0; 
  7.      
  8.     public function __construct(PDO $pdo) 
  9.     { 
  10.         session_set_save_handler( 
  11.             array(&$this, 'open'), 
  12.             array(&$this, 'close'), 
  13.             array(&$this, 'read'), 
  14.             array(&$this, 'write'), 
  15.             array(&$this, 'destroy'), 
  16.             array(&$this, 'gc')                                                             
  17.         ); 
  18.          
  19.         $this->_pdo   = $pdo; 
  20.         $this->_ip    = !emptyempty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; 
  21.         $this->_maxLifeTime = ini_get('session.gc_maxlifetime'); 
  22.     } 
  23.      
  24.     public function open($path,$name) 
  25.     { 
  26.         return true;     
  27.     } 
  28.      
  29.     public function close() 
  30.     { 
  31.         return true; 
  32.     } 
  33.      
  34.     public function read($id) 
  35.     { 
  36.         $sql = 'SELECT * FROM session where PHPSESSID = ?'; 
  37.         $stmt = $this->_pdo->prepare($sql); 
  38.         $stmt->execute(array($id)); 
  39.          
  40.         if (!$result = $stmt->fetch(PDO::FETCH_ASSOC)) { 
  41.             return null; 
  42.         } elseif ($this->_ip != $result['client_ip']) { 
  43.             return null; 
  44.         } elseif ($result['update_time']+$this->_maxLifeTime < time()){ 
  45.             $this->destroy($id); 
  46.             return null; 
  47.         } else { 
  48.             return $result['data'];     
  49.         } 
  50.     } 
  51.      
  52.     public function write($id,$data) 
  53.     { 
  54.         $sql = 'SELECT * FROM session where PHPSESSID = ?'; 
  55.         $stmt = $this->_pdo->prepare($sql); 
  56.         $stmt->execute(array($id)); 
  57.          
  58.         if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { 
  59.             if ($result['data'] != $data) { 
  60.                 $sql = 'UPDATE session SET update_time =? , date = ? WHERE PHPSESSID = ?'; 
  61.                  
  62.                 $stmt = $this->_pdo->prepare($sql); 
  63.                 $stmt->execute(array(time(), $data, $id)); 
  64.             } 
  65.         } else { 
  66.             if (!emptyempty($data)) { 
  67.                 $sql = 'INSERT INTO session (PHPSESSID, update_time, client_ip, data) VALUES (?,?,?,?)'; 
  68.                 $stmt = $this->_pdo->prepare($sql); 
  69.                 $stmt->execute(array($id, time(), $this->_ip, $data)); 
  70.             } 
  71.         } 
  72.          
  73.         return true; 
  74.     } 
  75.      
  76.     public function destroy($id) 
  77.     { 
  78.         $sql = 'DELETE FROM session WHERE PHPSESSID = ?'; 
  79.         $stmt = $this->_pdo->prepare($sql); 
  80.         $stmt->execute(array($id)); 
  81.          
  82.         return true;         
  83.     } 
  84.      
  85.     public function gc($maxLifeTime) 
  86.     { 
  87.         $sql = 'DELETE FROM session WHERE update_time < ?'; 
  88.         $stmt = $this->_pdo->prepare($sql); 
  89.         $stmt->execute(array(time() - $maxLifeTime)); 
  90.          
  91.         return true; 
  92.     } 
  93.  
  94. try{ 
  95.     $pdo = new PDO('mysql:host=localhost;dbname=rphp4zf', 'root','rickyfeng'); 
  96.     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
  97.      
  98.     new SessionToDB($pdo);     
  99. } catch(PDOException $e) { 
  100.     echo 'Error: '.$e->getMessage(); 

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.