将session写入数据库中

本文介绍了一种使用 PHP 将 Session 数据存储到 MySQL 数据库的方法。通过自定义 session handler 实现了 session 的开启、关闭、读取、写入、销毁及垃圾回收等功能,并确保了 IP 地址的一致性和 session 的有效期。

将session写入数据库中


<?php

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"] : 'unknown';
			self::$lifetime=ini_get('session.gc_maxlifetime');
			self::$time=time();
		}


		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($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'];


		}


		public static function write($PHPSESSID, $data){
			$sql="select * 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 = ?";
					
					$stm=self::$handler->prepare($sql);
					$stm->execute(array(self::$time, $data, $PHPSESSID));
				
				}
			}else{
				if(!empty($data)){
					$sql="insert into session(PHPSESSID, update_time, client_ip, data) values(?,?,?,?)";


					$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;
		}


		private static function gc($lifetime){
			$sql = "delete from session where update_time < ?";


			$stmt=self::$handler->prepare($sql);


			$stmt->execute(array(self::$time-$lifetime));


			return true;
		}	
	}


	try{
		$pdo=new PDO("mysql:host=localhost;dbname=xd", "root", "");
	}catch(PDOException $e){
		echo $e->getMessage();
	}


	Session::start($pdo);


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值