欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫

 更新時間:2015年07月26日 17:27:50   投稿:hebedich  
這篇文章主要介紹了php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下

使用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();    //獲取當(dāng)前時間
    }
    //調(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)容了,希望大家能夠喜歡。

相關(guān)文章

  • PHP時間類完整實(shí)例(非常實(shí)用)

    PHP時間類完整實(shí)例(非常實(shí)用)

    這篇文章主要介紹了PHP時間類完整實(shí)例,涉及PHP針對日期、時間、星期等的獲取與比較等操作技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-12-12
  • PHP goto語句用法實(shí)例

    PHP goto語句用法實(shí)例

    在本篇文章里小編給大家整理了關(guān)于PHP goto語句用法的相關(guān)實(shí)例以及語法講解,需要的朋友們參考學(xué)習(xí)下。
    2019-08-08
  • php5.4以下版本json不支持不轉(zhuǎn)義內(nèi)容中文的解決方法

    php5.4以下版本json不支持不轉(zhuǎn)義內(nèi)容中文的解決方法

    這篇文章主要介紹了php5.4以下版本json不支持不轉(zhuǎn)義內(nèi)容中文的解決方法,通過一個自定義php方法實(shí)現(xiàn)模擬joson中文不轉(zhuǎn)義,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • php實(shí)現(xiàn)遞歸的三種基本方式

    php實(shí)現(xiàn)遞歸的三種基本方式

    這篇文章主要介紹了php實(shí)現(xiàn)遞歸的三種基本方法,包括利用引用做參數(shù),利用全局變量,利用靜態(tài)變量來實(shí)現(xiàn)遞歸,并附上了相關(guān)示例,最后給大家一個演示,涉及php的遞歸操作技巧,需要的朋友可以參考下
    2015-08-08
  • php使用date和strtotime函數(shù)輸出指定日期的方法

    php使用date和strtotime函數(shù)輸出指定日期的方法

    這篇文章主要介紹了php使用date和strtotime函數(shù)輸出指定日期的方法,實(shí)例匯總了生成各種日期格式的方法,非常具有實(shí)用價值,需要的朋友可以參考下
    2014-11-11
  • 關(guān)于訪問控制的一首PHP面試題(對屬性或方法的訪問控制)

    關(guān)于訪問控制的一首PHP面試題(對屬性或方法的訪問控制)

    對屬性或方法的訪問控制,是通過在前面添加關(guān)鍵字 public、protected 或 private 來實(shí)現(xiàn)的
    2012-09-09
  • 解析phpstorm + xdebug 遠(yuǎn)程斷點(diǎn)調(diào)試

    解析phpstorm + xdebug 遠(yuǎn)程斷點(diǎn)調(diào)試

    本篇文章是對phpstorm + xdebug 遠(yuǎn)程斷點(diǎn)調(diào)試進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP+ajax分頁實(shí)例簡析

    PHP+ajax分頁實(shí)例簡析

    這篇文章主要介紹了PHP+ajax分頁實(shí)現(xiàn)方法,結(jié)合簡單實(shí)例形式分析了php數(shù)據(jù)查詢與ajax分頁的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • php啟動時候提示PHP startup的解決方法

    php啟動時候提示PHP startup的解決方法

    配置好php環(huán)境后,每次開機(jī)都有警告提示說 PHP startup,解決這個問題很簡單只需要在php.ini 文件中修改 extension_dir配置就行
    2013-05-05
  • php驗(yàn)證碼的制作思路和實(shí)現(xiàn)方法

    php驗(yàn)證碼的制作思路和實(shí)現(xiàn)方法

    這篇文章主要介紹了php驗(yàn)證碼的制作思路和實(shí)現(xiàn)方法,我們不能盲目的去實(shí)現(xiàn)php生成驗(yàn)證碼,更應(yīng)該了解php驗(yàn)證碼的基本原理,真正的掌握php驗(yàn)證碼的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-11-11

最新評論