Session保存到数据库的php类分享
2015-01-24信息快讯网
Session保存到数据库的php类,需要的朋友可以参考下。
<?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();
}
基于wordpress主题制作的具体实现步骤
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
PHP命名空间(Namespace)的使用详解
基于Zend的Config机制的应用分析
php中使用$_REQUEST需要注意的一个问题
PHP5中Cookie与 Session使用详解
基于curl数据采集之正则处理函数get_matches的使用
php中is_null,empty,isset,unset 的区别详细介绍
php中serialize序列化与json性能测试的示例分析
基于python发送邮件的乱码问题的解决办法
关于Iframe如何跨域访问Cookie和Session的解决方法
php简单开启gzip压缩方法(zlib.output_compression)
PHP中header和session_start前不能有输出原因分析
PHP 小心urldecode引发的SQL注入漏洞
php中批量修改文件后缀名的函数代码
如何在symfony中导出为CSV文件中的数据
linux iconv方法的使用
linux系统上支持php的 iconv()函数的方法
php json_encode奇怪问题说明
PHP下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
Sorting Array Values in PHP(数组排序)
php中json_encode中文编码问题分析
php正则表达式(regar expression)
PHP setcookie指定domain参数后,在IE下设置cookie失效的解决方法
PHP session会话的安全性分析
PHP IF ELSE简化/三元一次式的使用
PHP表单验证的3个函数ISSET()、empty()、is_numeric()的使用方法
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
php中设置多级目录session的问题
从手册去理解分析PHP session机制