session数据保存到mysql数据库中

/**
 * 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>";


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值