使用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="/tag/sql/" target="_blank" class="keywords">sql</a>);
$stmt->execute(array($<a href="/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="/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="/tag/sql/" target="_blank" class="keywords">sql</a>);
$stmt->execute(array($<a href="/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="/tag/sql/" target="_blank" class="keywords">sql</a> = "update session set update_time=?,data=? where <a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID = ?";
$stmt = self::$handler->prepare($<a href="/tag/sql/" target="_blank" class="keywords">sql</a>);
$stmt->execute(array($self::$time,$data,$<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID));
}
}else{
if(!empty($data)){
try{
$<a href="/tag/sql/" target="_blank" class="keywords">sql</a> = "insert into session(<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID,data) values(?,?,?)";
}catch(PDOException $e){
echo $e->getMessage();
}
$sth = self::$handler->prepare($<a href="/tag/sql/" target="_blank" class="keywords">sql</a>);
$sth->execute(array($<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID,self::$time,self::$ip,$data));
}
}
return true;
}
public static function destroy($<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID){
$<a href="/tag/sql/" target="_blank" class="keywords">sql</a> = "delete from session where <a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID = ?";
$stmt = self::$handler->prepare($<a href="/tag/sql/" target="_blank" class="keywords">sql</a>);
$stmt->execute(array($<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>SESSID));
return true;
}
public static function gc($lifetime){
$<a href="/tag/sql/" target="_blank" class="keywords">sql</a> = "delete from session where update_time<?";
$stmt = self::$handler->prepare($<a href="/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);
以上所述就是本文的全部内容了,希望大家能够喜欢。