自定义session储存 数据库 方式类
在php.ini配置文件中更改设置 (Registered_save_handlers 有三种方式 files user memcache)
session.save_handler = user 表示用户自定义session类<?php
/*
* 自定义session储存数据库方式的类
*
*/
class DBSession{
public static $pdo; //pdo的对象
public static $ctime; //当前时间
public static $maxlifetime; //最大生存时间
public static $uip; //用户正在用的ip
public static $uagent; //用户正在使用的浏览器
public static function start(PDO $pdo){//限制变量是PDO类
self::$pdo = $pdo;
self::$ctime = time();
self::$maxlifetime = ini_get('session.gc_maxlifetime');
//设置不同版本的浏览器获取ip方式
self::$uip = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] :
(!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR']:
(!empty($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:" "));
self::$uagent = !empty($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:' ';
//判断ip是否合法
// filter_var(self::$uip,FILTER_VALIDATE_IP) && self::$uip = ' ';
//注册自定义函数
session_set_save_handler(
array(__CLASS__,'open'),
array(__CLASS__,'close'),
array(__CLASS__,'read'),
array(__CLASS__,'write'),
array(__CLASS__,'destroy'),
array(__CLASS__, 'gc'));
//开启会话
session_start();
}
//开始 sesssion_start()
public static function open($path,$name){
echo 'open
';
return true;
}
//关闭
public static function close(){
echo 'close
';
return true;
}
//读取 echo
public static function read($sid){
echo 'read
';
$query = "select * from session where sid = ?";
$stmt = self::$pdo->prepare($query);
$stmt->execute(array($sid));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//还没有会话信息,返回空字符串
if(!$result){
return ' ';
}
//如果超出时间,销毁session
if($result['utime']+self::$maxlifetime
self::destroy($sid);
return ' ';
}
//如果用户换了IP或换了浏览器
if($result['uip']!=self::$uip || $result['uagent'] != self::$uagent){
self::destroy($sid);
return ' ';
}
return $result['sdata'];
}
//写入 $_SESSION['username']='yang'
public static function write($sid,$data){
echo 'write
';
$query = "select * from session where sid =? ";
$stmt = self::$pdo->prepare($query);
$stmt->execute(array($sid));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//若有数据,则更新
if($result){
//如果数据和原来的不一样 或者 当前时间大于文件修改时间加30秒的时候 才更新
if($result['sdata']!=$data || $result['utime']+30
$query = "update session set sdata = ?, utime = ? where sid =? ";
$stmt = self::$pdo->prepare($query);
$stmt->execute(array($data,self::$ctime,$sid));
}
}else{//插入数据
if(!empty($data)){
$query = "insert into session(sid,sdata,utime,uip,uagent) values(?,?,?,?,?)";
$stmt = self::$pdo->prepare($query);
$stmt -> execute(array($sid,$data,self::$ctime,self::$uip,self::$uagent));
}
}
}
//销毁 session_destroy
public static function destroy($sid){
echo 'destroy
';
$query = "delete from session where sid = ?";
$stmt = self::$pdo->prepare($query);
return $stmt -> execute(array($sid));
}
//回收垃圾
public static function gc($maxlifetime){
echo 'gc
';
$query = "delete from session where utime
$stmt = self::$pdo->prepare($query);
return $stmt -> execute(array(self::$ctime-self::$maxlifetime));
}
}
DBSession::start($pdo);
创建session表create table session(
sid char(32) not null default ' ',
utime int not null default 0,
sdata text,
uip char(15) not null default ' ',
uagent varchar(200) not null default ' ',
index session_sid(sid)
);