国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

將Session寫入數據庫

2019-11-15 02:09:29
字體:
來源:轉載
供稿:網友
session寫入數據庫

使用session_set_save_handler()函數,將Session的內容寫入數據庫

  1 <?php  2     /*  3     *@author    Fahy  4     *@link    http://home.VEVb.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);


上一篇:這樣用01串

下一篇:將Session寫入Memcache

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 横峰县| 崇文区| 托克逊县| 高尔夫| 天峨县| 清涧县| 广元市| 昌乐县| 昌吉市| 宁津县| 高唐县| 汾阳市| 五峰| 珲春市| 西藏| 乐东| 湾仔区| 江阴市| 延吉市| 盖州市| 阿图什市| 安达市| 杭锦后旗| 威宁| 子长县| 阳江市| 毕节市| 邢台市| 兴国县| 阿尔山市| 务川| 凭祥市| 灵山县| 峨边| 三都| 贵南县| 揭东县| 南召县| 黑水县| 宁蒗| 墨玉县|