Session保存到數(shù)據(jù)庫(kù)的php類分享
更新時(shí)間:2011年10月24日 00:17:35 作者:
Session保存到數(shù)據(jù)庫(kù)的php類,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php
class SessionToDB
{
private $_path = null;
private $_name = null;
private $_pdo = null;
private $_ip = null;
private $_maxLifeTime = 0;
public function __construct(PDO $pdo)
{
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
$this->_pdo = $pdo;
$this->_ip = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
$this->_maxLifeTime = ini_get('session.gc_maxlifetime');
}
public function open($path,$name)
{
return true;
}
public function close()
{
return true;
}
public function read($id)
{
$sql = 'SELECT * FROM session where PHPSESSID = ?';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array($id));
if (!$result = $stmt->fetch(PDO::FETCH_ASSOC)) {
return null;
} elseif ($this->_ip != $result['client_ip']) {
return null;
} elseif ($result['update_time']+$this->_maxLifeTime < time()){
$this->destroy($id);
return null;
} else {
return $result['data'];
}
}
public function write($id,$data)
{
$sql = 'SELECT * FROM session where PHPSESSID = ?';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array($id));
if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($result['data'] != $data) {
$sql = 'UPDATE session SET update_time =? , date = ? WHERE PHPSESSID = ?';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array(time(), $data, $id));
}
} else {
if (!empty($data)) {
$sql = 'INSERT INTO session (PHPSESSID, update_time, client_ip, data) VALUES (?,?,?,?)';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array($id, time(), $this->_ip, $data));
}
}
return true;
}
public function destroy($id)
{
$sql = 'DELETE FROM session WHERE PHPSESSID = ?';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array($id));
return true;
}
public function gc($maxLifeTime)
{
$sql = 'DELETE FROM session WHERE update_time < ?';
$stmt = $this->_pdo->prepare($sql);
$stmt->execute(array(time() - $maxLifeTime));
return true;
}
}
try{
$pdo = new PDO('mysql:host=localhost;dbname=rphp4zf', 'root','rickyfeng');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
new SessionToDB($pdo);
} catch(PDOException $e) {
echo 'Error: '.$e->getMessage();
}
您可能感興趣的文章:
- 將PHP的session數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中的代碼實(shí)例
- php session 寫入數(shù)據(jù)庫(kù)
- php基于session實(shí)現(xiàn)數(shù)據(jù)庫(kù)交互的類實(shí)例
- php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫(kù)
- PHP將session信息存儲(chǔ)到數(shù)據(jù)庫(kù)的類實(shí)例
- php中使用session_set_save_handler()函數(shù)把session保存到MySQL數(shù)據(jù)庫(kù)實(shí)例
- PHP獨(dú)立Session數(shù)據(jù)庫(kù)存儲(chǔ)操作類分享
- php把session寫入數(shù)據(jù)庫(kù)示例
- PHP用mysql數(shù)據(jù)庫(kù)存儲(chǔ)session的代碼
- PHP封裝的數(shù)據(jù)庫(kù)保存session功能類
相關(guān)文章
Windows2003 下 MySQL 數(shù)據(jù)庫(kù)每天自動(dòng)備份
Windows2003 下 MySQL 數(shù)據(jù)庫(kù)每天自動(dòng)備份...2006-12-12
PHP中檢索字符串的方法分析【strstr與substr_count方法】
這篇文章主要介紹了PHP中檢索字符串的方法,結(jié)合實(shí)例形式分析了strstr與substr_count函數(shù)的功能與具體使用技巧,需要的朋友可以參考下2017-02-02
PHP設(shè)計(jì)模式之建造者模式定義與用法簡(jiǎn)單示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之建造者模式定義與用法,簡(jiǎn)單描述了建造者模式的概念、原理并結(jié)合實(shí)例形式分析了建造者模式的具體定義與使用方法,需要的朋友可以參考下2018-08-08
php中鉤子(hook)的原理與簡(jiǎn)單應(yīng)用demo示例
這篇文章主要介紹了php中鉤子(hook)的原理與簡(jiǎn)單應(yīng)用,結(jié)合完整demo實(shí)例形式分析了php中鉤子(hook)的原理及簡(jiǎn)單使用操作技巧,需要的朋友可以參考下2019-09-09
PHP的Yii框架中Model模型的學(xué)習(xí)教程
這篇文章主要介紹了PHP的Yii框架中Model模型的學(xué)習(xí)教程,Yii框架本身就顯龐大,所以模型類也就更加需要很好的編寫維護(hù),需要的朋友可以參考下2016-03-03

