成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術(shù)教程 ? php實現(xiàn)將Session寫入數(shù)據(jù)庫
全站頭部文字 我要出現(xiàn)在這里
php實現(xiàn)將Session寫入數(shù)據(jù)庫 858 2023-12-08   

使用session_set_save_handler()函數(shù),將Session的內(nèi)容寫入數(shù)據(jù)庫

<?php
  /*
  *@author  Fahy
  *數(shù)據(jù)庫為mysql,
  *數(shù)據(jù)庫名為session,表名為session,
  *表中字段包括PHPSESSID,update_time,client_ip,data
  */
  class Session{
    private static $handler = null;
    private static $ip = null;
    private static $lifetime = null;
    private static $time = null;
    
    //配置靜態(tài)變量
    private static function init($handler){
      self::$handler = $handler;    //獲取數(shù)據(jù)庫資源
      self::$ip = !empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:'unkonw';    //獲取客戶端ip
      self::$lifetime = ini_get('session.gc_maxlifetime');    //獲取session生命周期
      self::$time = time();    //獲取當前時間
    }
    //調(diào)用session_set_save_handler()函數(shù)并開啟session
    static function start($pdo){
      self::init($pdo);
      session_set_save_handler(
        array(__CLASS__,'open'),
        array(__CLASS__,'close'),
        array(__CLASS__,'read'),
        array(__CLASS__,'write'),
        array(__CLASS__,'destroy'),
        array(__CLASS__,'gc')
      );
      session_start();
    }
    
    public static function open($path,$name){
      return true;
    }
    public static function close(){
      return true;
    }
    
    //查詢數(shù)據(jù)庫中的數(shù)據(jù)
    public static function read($PHPSESSID){
      $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?";
      $stmt = self::$handler->prepare($sql);
      $stmt->execute(array($PHPSESSID));
      if(!$result = $stmt->fetch(PDO::FETCH_ASSOC)){
        return '';
      }
      if(self::$ip == $result['client_ip']){
        self::destroy($PHPSESSID);
        return '';
      }
      if(($result['update_time']+self::$lifetime)<self::$time){
        self::destroy($PHPSESSID);
        return '';
      }
      return $result['data'];
    }
    /*
    *首先查詢該session是否存在數(shù)據(jù),如果存在,則更新數(shù)據(jù),如果不存在,則插入數(shù)據(jù)
    */
    //將session寫入數(shù)據(jù)庫中,$data傳入session中的keys和values數(shù)組
    public static function write($PHPSESSID,$data){
      $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESSID=?";
      $stmt = self::$handler->prepare($sql);
      $stmt->execute(array($PHPSESSID));
      
      if($result=$stmt->fetch(PDO::FETCH_ASSOC)){        
        if($result['data'] != $data || self::$time > ($result['update_time']+30)){
          $sql = "update session set update_time=?,data=? where PHPSESSID = ?";
          $stmt = self::$handler->prepare($sql);
          $stmt->execute(array($self::$time,$data,$PHPSESSID));
        }
      }else{
        if(!empty($data)){
          try{
            $sql = "insert into session(PHPSESSID,update_time,client_ip,data) values(?,?,?,?)";
          }catch(PDOException $e){
            echo $e->getMessage();
          }
          $sth = self::$handler->prepare($sql);
          $sth->execute(array($PHPSESSID,self::$time,self::$ip,$data));
        }
      }
      return true;
    }
    
    public static function destroy($PHPSESSID){
      $sql = "delete from session where PHPSESSID = ?";
      $stmt = self::$handler->prepare($sql);
      $stmt->execute(array($PHPSESSID));
      return true;
    }
    public static function gc($lifetime){
      $sql = "delete from session where update_time<?";
      $stmt = self::$handler->prepare($sql);
      $stmt->execute(array(self::$time-$lifetime));
      return true;
    }
  }
  //使用PDO連接數(shù)據(jù)庫
  try{
    $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193");
  }catch(PDOException $e){
    echo $e->getMessage();
  }
  //傳遞數(shù)據(jù)庫資源
  Session::start($pdo);

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。


主站蜘蛛池模板: 赖小子| 成龙电影全集大全| 拔萝卜视频免费播放| 补充电解质喝什么饮料| 网上视频| 韩国电影色即是空| 都市频道在线直播回放| 圣般若摄颂| 风间由美的作品| 公主抱的图片| xiuren秀人网最新地址| 谢承均| 我的神我要赞美你| 色戒观看| 隐藏的歌手中国版全集| 兔子电影| 红星闪闪歌词完整版打印| 出轨的女人电影| 一个都不能少电影| 山西卫视直播高清在线直播| 高一英语单词表电子版| 欧美17p| 间宫祥太朗| 电影壮志凌云女版满天星法版在线看| 梁祝吉他谱独奏完整| 日韩女同性恋| 裸体摸特| 速度与激情15| 同性gay在线| 难兄难弟 电视剧| 成品直播大全免费观看| 《水中花》日本电影| 诗歌繁星| 夜魔3| 白昼先生 电影| 就爱小姐姐| cctv第一剧场频道| 洛城僵尸| 欲海浮沉| 17岁韩国电影完整版观看免费| 大尺度激情吻戏|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103