99久久国产露脸精品麻豆,欧美日韩精品小说,亚洲免费在线美女视频,国产三级中文字幕,91极品国产情侣高潮对白,国产亚洲一区二区三区不卡片,欧美jizz精品欧美性,久久国产精品久久国产片

session的學習筆記 —— 將session寫入數據庫

袁志蒙 3618次瀏覽

摘要:學習了一天,寫了一個session類,將session寫入數據庫,方便以后用,記錄一下...

學習了一天,寫了一個session類,將session寫入數據庫,方便以后用,記錄一下


代碼分享:

<?php
/*
* 說明:
* 數據庫為mysql,
* 數據庫名為session,表名為session,
* 表中字段包括PHPSESSID,update_time,client_ip,data
* 版權所有:(c) 2014-2016 https://www.yzmcms.com All rights reserved.
*/

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"]:'unkonw';        //獲取客戶端ip
		self::$lifetime = ini_get('session.gc_maxlifetime');        //獲取session生命周期
		self::$time = time();        //獲取當前時間
	}
	
	//調用session_set_save_handler()函數并開啟session
	static function start($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'];
	}
	
	/*
	 *首先查詢該session是否存在數據,如果存在,則更新數據,如果不存在,則插入數據
	 *將session寫入數據庫中,$data傳入session中的keys和values數組
	 */
	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 = ?";
				 $stmt = self::$handler->prepare($sql);
				 $stmt->execute(array($self::$time,$data,$PHPSESSID));
			}
		 }else{
			 if(!empty($data)){
				 try{
					 $sql = "insert into session(PHPSESSID,update_time,client_ip,data) values(?,?,?,?)";
				 }catch(PDOException $e){
					 echo $e->getMessage();
				 }
				 $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;
	}
	
	public static function gc($lifetime){
		$sql = "delete from session where update_time<?";
		$stmt = self::$handler->prepare($sql);
		$stmt->execute(array(self::$time-$lifetime));
		return true;
	}
}

//使用PDO連接數據庫
try{
	$pdo = new PDO("mysql:host=localhost;dbname=session","root","root");
}catch(PDOException $e){
	echo $e->getMessage();
}

//開啟session
session::start($pdo);
隨機內容

表情

共1條評論
  • 網友評論:

    試試功能,搶沙發

    2016-12-20 15:23:47 回復

    點擊加載