Session保存到数据库的php类分享
作者:bea
代码如下: <?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(&a
代码如下:
<?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();
}
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 使用PHPRPC实现Ajax安全登录
- 求PHP数组最大值,最小值的代码
- php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位
- php数组函数序列之end() - 移动数组内部指针到最后一个元素,并返回该元素的值
- php数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
- php数组函数序列之next() - 移动数组内部指针到下一个元素的位置,并返回该元素值
- php数组函数序列之array_values() 获取数组元素值的函数与方法
- php数组函数序列之array_keys() - 获取数组键名
- php数组函数序列之array_combine() - 数组合并函数使用说明
- php数组函数序列之in_array() 查找数组值是否存在
- php数组函数序列之array_unique() - 去除数组中重复的元素值
- php数组函数序列之array_sum() - 计算数组元素值之和
- php数组函数序列之array_key_exists() - 查找数组键名是否存在
- php数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- 让Json更懂中文(JSON_UNESCAPED_UNICODE)
- PHP实现异步调用方法研究与分享
- 提示Trying to clone an uncloneable object of class Imagic的解决
- PHP 小心urldecode引发的SQL注入漏洞