今天我们就先介绍一个SESSION写到数据库的类,都是面向对象的大家看懂就行,然后我会到以后一点一点的基础写,写到大家懂,现在就是大家记住了,以后看基础的时候恍然大悟原来这样呀!啊哈哈,代码

//简单的数据库,大家测试用
create table session(phpsessid,varchar(32), update_time int(10), client_ip char(15), data text);
<?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 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 = ?";
					
					$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=xw", "root", "dgj99349");
	}catch(PDOException $e){
		echo $e->getMessage();
	}

	Session::start($pdo);

欢迎转载,转载请注明来自微度网络-网络技术中心http://yun.widuu.com

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部