您好,登錄后才能下訂單哦!
自定義session儲存 數(shù)據(jù)庫 方式類
在php.ini配置文件中更改設(shè)置 (Registered_save_handlers 有三種方式 files user memcache)
session.save_handler = user 表示用戶自定義session類
<?php /* * 自定義session儲存數(shù)據(jù)庫方式的類 * */ class DBSession{ public static $pdo; //pdo的對象 public static $ctime; //當(dāng)前時間 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'); //設(shè)置不同版本的瀏覽器獲取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 = ' '; //注冊自定義函數(shù) 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<br/>'; return true; } //關(guān)閉 public static function close(){ echo 'close<br/>'; return true; } //讀取 echo public static function read($sid){ echo 'read<br/>'; $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::$ctime){ 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<br/>'; $query = "select * from session where sid =? "; $stmt = self::$pdo->prepare($query); $stmt->execute(array($sid)); $result = $stmt->fetch(PDO::FETCH_ASSOC); //若有數(shù)據(jù),則更新 if($result){ //如果數(shù)據(jù)和原來的不一樣 或者 當(dāng)前時間大于文件修改時間加30秒的時候 才更新 if($result['sdata']!=$data || $result['utime']+30< self::$ctime){// +加號優(yōu)先級高于 <小于號 $query = "update session set sdata = ?, utime = ? where sid =? "; $stmt = self::$pdo->prepare($query); $stmt->execute(array($data,self::$ctime,$sid)); } }else{//插入數(shù)據(jù) 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<br/>'; $query = "delete from session where sid = ?"; $stmt = self::$pdo->prepare($query); return $stmt -> execute(array($sid)); } //回收垃圾 public static function gc($maxlifetime){ echo 'gc<br/>'; $query = "delete from session where utime < ?"; $stmt = self::$pdo->prepare($query); return $stmt -> execute(array(self::$ctime-self::$maxlifetime)); } } DBSession::start($pdo);
創(chuàng)建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) );
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。