php實現(xiàn)將Session寫入數(shù)據(jù)庫
更新時間:2015年07月26日 17:27:50 投稿:hebedich
這篇文章主要介紹了php實現(xiàn)將Session寫入數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下
使用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ù)存儲到數(shù)據(jù)庫中的代碼實例
- php session 寫入數(shù)據(jù)庫
- php基于session實現(xiàn)數(shù)據(jù)庫交互的類實例
- PHP將session信息存儲到數(shù)據(jù)庫的類實例
- php中使用session_set_save_handler()函數(shù)把session保存到MySQL數(shù)據(jù)庫實例
- PHP獨立Session數(shù)據(jù)庫存儲操作類分享
- php把session寫入數(shù)據(jù)庫示例
- Session保存到數(shù)據(jù)庫的php類分享
- PHP用mysql數(shù)據(jù)庫存儲session的代碼
- PHP封裝的數(shù)據(jù)庫保存session功能類
相關(guān)文章
PHP使用strrev翻轉(zhuǎn)中文亂碼問題的解決方法
這篇文章主要介紹了PHP使用strrev翻轉(zhuǎn)中文亂碼問題的解決方法,通過自定義函數(shù)遍歷字符串并設(shè)置編碼格式解決亂碼問題,需要的朋友可以參考下2017-01-01
PHP 多進程與信號中斷實現(xiàn)多任務(wù)常駐內(nèi)存管理實例方法
在本篇文章里小編給大家整理的是關(guān)于PHP 多進程與信號中斷實現(xiàn)多任務(wù)常駐內(nèi)存管理的相關(guān)知識點,有需要的朋友們學(xué)習(xí)下。2019-10-10
php實現(xiàn)通過stomp協(xié)議連接ActiveMQ操作示例
這篇文章主要介紹了php實現(xiàn)通過stomp協(xié)議連接ActiveMQ操作,結(jié)合實例形式分析了stomp擴展安裝及使用stomp擴展連接ActiveMQ具體操作技巧,需要的朋友可以參考下2020-02-02

