php实现将Session写入数据库

yipeiwu_com5年前PHP代码库

使用session_set_save_handler()函数,将Session的内容写入数据库

<?php
  /*
  *@author  Fahy
  *数据库为mysql,
  *数据库名为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;
    
    //配置静态变量
    private static function init($handler){
      self::$handler = $handler;    //获取数据库资源
      self::$ip = !empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:'unkonw';    //获取客户端ip
      self::$lifetime = ini_get('session.gc_maxlifetime');    //获取session生命周期
      self::$time = time();    //获取当前时间
    }
    //调用session_set_save_handler()函数并开启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;
    }
    
    //查询数据库中的数据
    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是否存在数据,如果存在,则更新数据,如果不存在,则插入数据
    */
    //将session写入数据库中,$data传入session中的keys和values数组
    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连接数据库
  try{
    $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193");
  }catch(PDOException $e){
    echo $e->getMessage();
  }
  //传递数据库资源
  Session::start($pdo);

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

PHP二维数组去重算法

需求 现在有下面一组二维数组: array(8) { [0]=> array(2) { ["name"]=> string(4) "name" ["va...

php正则表达式基本知识与应用详解【经典教程】

本文实例讲述了php正则表达式基本知识与应用。分享给大家供大家参考,具体如下: 概述 正则表达式是一种描述字符串结果的语法规则,是一个特定的格式化模式,可以匹配、替换、截取匹配的字符串。...

详解php 使用Callable Closure强制指定回调类型

详解php 使用Callable Closure强制指定回调类型 如果一个方法需要接受一个回调方法作为参数,我们可以这样写 <?php function testC...

php获取URL中带#号等特殊符号参数的解决方法

例如下面的PHP代码: 复制代码 代码如下: <?php echo $_GET['key']; ?> 当url为http://test.com/c.php&...

PHP远程连接oracle数据库操作实现方法图文详解

PHP远程连接oracle数据库操作实现方法图文详解

本文实例讲述了PHP远程连接oracle数据库操作实现方法。分享给大家供大家参考,具体如下: 一.以下是基于 wampServer 的php 访问oracle数据库的操作步骤: 1、第一...