欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php实现将Session写入数据库

程序员文章站 2023-12-01 23:37:40
使用session_set_save_handler()函数,将session的内容写入数据库

使用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);

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