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)文章
Windows2003 下 MySQL 數(shù)據(jù)庫每天自動備份
Windows2003 下 MySQL 數(shù)據(jù)庫每天自動備份...2006-12-12
PHP中檢索字符串的方法分析【strstr與substr_count方法】
這篇文章主要介紹了PHP中檢索字符串的方法,結(jié)合實例形式分析了strstr與substr_count函數(shù)的功能與具體使用技巧,需要的朋友可以參考下2017-02-02
php中鉤子(hook)的原理與簡單應(yīng)用demo示例
這篇文章主要介紹了php中鉤子(hook)的原理與簡單應(yīng)用,結(jié)合完整demo實例形式分析了php中鉤子(hook)的原理及簡單使用操作技巧,需要的朋友可以參考下2019-09-09

