今天就跟大家聊聊有关利用php怎么将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);
看完上述内容,你们对利用php怎么将Session写入到数据库中有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。