/**
* session数据保存到数据库中
* 使用session_set_save_handler 函数
* php.ini 设置
* 默认为session.save_handler = files,必须将其修改为session.save_handler = user,即变为用户自定义方式。
*/
class SessionMysql{
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"] : "unknown";
self::$lifetime=ini_get('session.gc_maxlifetime');
self::$time=time();
}
public static function start(PDO $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($id){
echo "read读取信息<br>";
$sql="select sessionid,updatetime,client_ip,data from session where sessionid= ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($id));
if(!$result=$stmt->fetch(PDO::FETCH_ASSOC)){
echo "无内容";
return "";
}
if (self::$ip !=$result["client_ip"]){
echo "ip不对";
self::destroy($id);
return '';
}
echo $result["updatetime"]."<br>";
echo self::$lifetime."<br>";
echo ($result["updatetime"]+self::$lifetime)."<br>";
if (($result["updatetime"]+self::$lifetime) < self::$time){
echo "时间过期";
self::destroy($id);
return '';
}
return $result['data'];
}
public static function write($id ,$data){
echo "write写信息<br>";
$sql="select sessionid,updatetime,client_ip,data from session where sessionid= ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($id));
if($result=$stmt->fetch(PDO::FETCH_ASSOC)){
if($result['data'] != $data || self::$time > ($result['updatetime']+300)){
$sql="update session set updatetime = ?, data =? where sessionid = ?";
$stm=self::$handler->prepare($sql);
$stm->execute(array(self::$time, $data, $id));
}
}else {
if(!empty($data)){
$sql="insert into session(sessionid, updatetime, client_ip, data) values(?,?,?,?)";
$sth=self::$handler->prepare($sql);
$sth->execute(array($id, self::$time, self::$ip, $data));
}
}
return true;
}
public static function destroy($id){
echo "destroy清除信息<br>";
$sql="delete from session where sessionid = ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($id));
return true;
}
private static function gc($lifetime){
echo "gc回收信息<br>";
$sql = "delete from session where updatetime < ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array(self::$time-$lifetime));
return true;
}
}
try {
$pdo=new PDO("mysql:host=localhost;dbname=wenzxx", "root", "admin");
} catch (PDOException $e) {
echo $e->getMessage();
}
SessionMysql::start($pdo);
调用文件1 mysql1.php
include 'SessionMysql.class.php';
$_SESSION["islogin3"] = 1;
$_SESSION["username"] = "admin";
$_SESSION["uid"] = 333;
echo session_name().'='.session_id()."<br>";
echo time();
调用文件2 mysql2.php
include 'SessionMysql.class.php';
print_r($_SESSION);
echo "<br>";
echo $_SERVER["REMOTE_ADDR"];
echo "<br>";
//echo session_name().'='.session_id()."<br>";
echo time();
调用文件3 mysql.3php
include 'SessionMysql.class.php';
$_SESSION = array();
if (isset($_COOKIE[session_name()])){
setcookie(session_name(), '' , time()-100, '/');
}
session_destroy();
echo session_name().'='.session_id()."<br>";