Session保存到數(shù)據(jù)庫的php類分享
更新時間:2011年10月24日 00:17:35 作者:
Session保存到數(shù)據(jù)庫的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ù)存儲到數(shù)據(jù)庫中的代碼實例
- php session 寫入數(shù)據(jù)庫
- php基于session實現(xiàn)數(shù)據(jù)庫交互的類實例
- php實現(xiàn)將Session寫入數(shù)據(jù)庫
- PHP將session信息存儲到數(shù)據(jù)庫的類實例
- php中使用session_set_save_handler()函數(shù)把session保存到MySQL數(shù)據(jù)庫實例
- PHP獨立Session數(shù)據(jù)庫存儲操作類分享
- php把session寫入數(shù)據(jù)庫示例
- PHP用mysql數(shù)據(jù)庫存儲session的代碼
- PHP封裝的數(shù)據(jù)庫保存session功能類
相關(guān)文章
php設(shè)計模式之單例、多例設(shè)計模式的應(yīng)用分析
本篇文章是對php設(shè)計模式中的單例與多例設(shè)計模式的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-06-06php 使用curl模擬ip和來源進行訪問的實現(xiàn)方法
下面小編就為大家?guī)硪黄猵hp 使用curl模擬ip和來源進行訪問的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05PHP去除數(shù)組中重復(fù)的元素并按鍵名排序函數(shù)
用php實現(xiàn)的去除數(shù)組中重復(fù)的函數(shù)2008-08-08php數(shù)組實現(xiàn)根據(jù)某個鍵值將相同鍵值合并生成新二維數(shù)組的方法
這篇文章主要介紹了php數(shù)組實現(xiàn)根據(jù)某個鍵值將相同鍵值合并生成新二維數(shù)組的方法,涉及php數(shù)組的遍歷、賦值相關(guān)運算技巧,需要的朋友可以參考下2017-04-04PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法詳解
本篇文章是對PHP CodeBase:將時間顯示為"剛剛""n分鐘/小時前"的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06