html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

Session保存到数据库_PHP教程_编程技术

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


F3kHTML5中文学习网 - HTML5先行者学习网
F3kHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助