PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php实现将Session写入数据库大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

php;">
//配置静态变量
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'),'read'),'write'),'destroy'),'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,data from session where phpSESSID=?";
  $stmt = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
  $stmt->execute(array($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID));
  if(!$result = $stmt->fetch(PDO::FETCH_ASSOC)){
    return '';
  }
  if(self::$ip == $result['client_ip']){
    self::destroy($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID);
    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,data from session where phpSESSID=?";
  $stmt = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
  $stmt->execute(array($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID));

  if($result=$stmt->fetch(PDO::FETCH_ASSOC)){        
    if($result['data'] != $data || self::$time > ($result['update_time']+30)){
      $<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a> = "update session set update_time=?,data=? where <a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID = ?";
      $stmt = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
      $stmt->execute(array($self::$time,$data,$<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID));
    }
  }else{
    if(!empty($data)){
      try{
        $<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a> = "insert into session(<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID,data) values(?,?,?)";
      }catch(PDOException $E){
        echo $e->getmessage();
      }
      $sth = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
      $sth->execute(array($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID,self::$time,self::$ip,$data));
    }
  }
  return true;
}

public static function destroy($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID){
  $<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a> = "delete from session where <a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID = ?";
  $stmt = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
  $stmt->execute(array($<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>SESSID));
  return true;
}
public static function gc($lifetimE){
  $<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a> = "delete from session where update_time<?";
  $stmt = self::$handler->prepare($<a href="http://code.js-code.com/tag/sql/" target="_blank" class="keywords">sql</a>);
  $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实现将Session写入数据库全部内容,希望文章能够帮你解决php实现将Session写入数据库所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:php