将Session写入数据库

使用session_set_save_handler()函数,将Session的内容写入数据库

  1 <?php
  2     /*
  3     *@author    Fahy
  4     *@link    http://home.cnblogs.com/u/HuangWj
  5     *数据库为mysql,
  6     *数据库名为session,表名为session,
  7     *表中字段包括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         //配置静态变量
 16         private static function init($handler){
 17             self::$handler = $handler;        //获取数据库资源
 18             self::$ip = !empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:‘unkonw‘;        //获取客户端ip
 19             self::$lifetime = ini_get(‘session.gc_maxlifetime‘);        //获取session生命周期
 20             self::$time = time();        //获取当前时间
 21         }
 22         //调用session_set_save_handler()函数并开启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__,‘destroy‘),
 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         //查询数据库中的数据
 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          *首先查询该session是否存在数据,如果存在,则更新数据,如果不存在,则插入数据
 63          */
 64         //将session写入数据库中,$data传入session中的keys和values数组
 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                      $sth = self::$handler->prepare($sql);
 84                      $sth->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     //使用PDO连接数据库
104     try{
105         $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193");
106     }catch(PDOException $e){
107         echo $e->getMessage();
108     }
109     //传递数据库资源
110     Session::start($pdo);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。