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 寫(xiě)入數(shù)據(jù)庫(kù)
- php基于session實(shí)現(xiàn)數(shù)據(jù)庫(kù)交互的類實(shí)例
- php實(shí)現(xiàn)將Session寫(xiě)入數(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寫(xiě)入數(shù)據(jù)庫(kù)示例
- PHP用mysql數(shù)據(jù)庫(kù)存儲(chǔ)session的代碼
- PHP封裝的數(shù)據(jù)庫(kù)保存session功能類
相關(guān)文章
php設(shè)計(jì)模式之單例、多例設(shè)計(jì)模式的應(yīng)用分析
本篇文章是對(duì)php設(shè)計(jì)模式中的單例與多例設(shè)計(jì)模式的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php 使用curl模擬ip和來(lái)源進(jìn)行訪問(wèn)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇php 使用curl模擬ip和來(lái)源進(jìn)行訪問(wèn)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05PHP去除數(shù)組中重復(fù)的元素并按鍵名排序函數(shù)
用php實(shí)現(xiàn)的去除數(shù)組中重復(fù)的函數(shù)2008-08-08php數(shù)組實(shí)現(xiàn)根據(jù)某個(gè)鍵值將相同鍵值合并生成新二維數(shù)組的方法
這篇文章主要介紹了php數(shù)組實(shí)現(xiàn)根據(jù)某個(gè)鍵值將相同鍵值合并生成新二維數(shù)組的方法,涉及php數(shù)組的遍歷、賦值相關(guān)運(yùn)算技巧,需要的朋友可以參考下2017-04-04PHP CodeBase:將時(shí)間顯示為"剛剛""n分鐘/小時(shí)前"的方法詳解
本篇文章是對(duì)PHP CodeBase:將時(shí)間顯示為"剛剛""n分鐘/小時(shí)前"的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP簡(jiǎn)單計(jì)算兩個(gè)時(shí)間差的方法示例
這篇文章主要介紹了PHP簡(jiǎn)單計(jì)算兩個(gè)時(shí)間差的方法,結(jié)合具體實(shí)例形式分析了php日期與時(shí)間的轉(zhuǎn)換及數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-06-06