將Session寫入資料庫,Session寫入資料庫_PHP教程

來源:互聯網
上載者:User

將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)$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);

http://www.bkjia.com/PHPjc/980029.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/980029.htmlTechArticle將Session寫入資料庫,Session寫入資料庫 使用session_set_save_handler()函數,將Session的內容寫入資料庫 1 ? php 2 /* 3 *@author Fahy 4 *@link http://home.cnb...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.