程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 將Session寫入數據庫,Session寫入數據庫

將Session寫入數據庫,Session寫入數據庫

編輯:關於PHP編程

將Session寫入數據庫,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);

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved