您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)利用php怎么將Session寫入到數(shù)據(jù)庫中,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
使用session_set_save_handler()函數(shù),將Session的內(nèi)容寫入數(shù)據(jù)庫
<?php /* *@author Fahy *數(shù)據(jù)庫為mysql, *數(shù)據(jù)庫名為session,表名為session, *表中字段包括PHPSESSID,update_time,client_ip,data */ class Session{ private static $handler = null; private static $ip = null; private static $lifetime = null; private static $time = null; //配置靜態(tài)變量 private static function init($handler){ self::$handler = $handler; //獲取數(shù)據(jù)庫資源 self::$ip = !empty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:'unkonw'; //獲取客戶端ip self::$lifetime = ini_get('session.gc_maxlifetime'); //獲取session生命周期 self::$time = time(); //獲取當(dāng)前時間 } //調(diào)用session_set_save_handler()函數(shù)并開啟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; } //查詢數(shù)據(jù)庫中的數(shù)據(jù) 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是否存在數(shù)據(jù),如果存在,則更新數(shù)據(jù),如果不存在,則插入數(shù)據(jù) */ //將session寫入數(shù)據(jù)庫中,$data傳入session中的keys和values數(shù)組 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連接數(shù)據(jù)庫 try{ $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193"); }catch(PDOException $e){ echo $e->getMessage(); } //傳遞數(shù)據(jù)庫資源 Session::start($pdo);
看完上述內(nèi)容,你們對利用php怎么將Session寫入到數(shù)據(jù)庫中有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。