<?php
class session
{
private static $handle = null;
private static $ip = null;
private static $lifetime = null;
private static $time = null; static function init($pdo)
{
self::$handle = $pdo;
self::$ip = !empty($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : "unknow";
self::$lifetime = ini_get('session.gc_maxlifetime');
self::$time = time();
} static function start($pdo)
{
self::init($pdo);
session_set_save_handler(
array(__CLASS__,'open'),
array(__CLASS__,'close'),
array(__CLASS__,'read'),
array(__CLASS__,'write'),
array(__CLASS__,'destroy'),
array(__CLASS__,'gc')
);
session_start();
} public static function open($path, $name)
{
return true;
} public static function close()
{
return true;
} public static function read($PHPSESSID)
{
$sql = "select * from session where PHPSESSID = ?";
$smit = self::$handle->prepare($sql);
$smit->execute(array($PHPSESSID)); if(!$result = $smit->fetch(PDO::FETCH_ASSOC))
{
return '';
} if(self::$ip != $result['client_ip'])
{
self::destroy($PHPSESSID);
return '';
} if( ($result['update_time'] + self::$lifetime) < self::$time)
{
self::destroy($PHPSESSID);
return '';
} return $result['data'];
} public static function write($PHPSESSID,$data)
{
$sql = "select * from session where PHPSESSID = ?";
$stmt = self::$handle->prepare($sql);
$stmt->execute(array($PHPSESSID)); if($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
if($result['data'] != $data || self::$time-30 > $result['update_time'])
{
$sql="update session set update_time = ?, data =? where PHPSESSID = ?";
$stmt = self::$handle->prepare($sql);
$stmt->execute(array(self::$time,$data,$PHPSESSID));
} }else
{
if(!empty($data))
{
$sql="insert into session(PHPSESSID, update_time, client_ip, data) values(?,?,?,?)";
$sth = self::$handle->prepare($sql);
$sth->execute(array($PHPSESSID,self::$time,self::$ip,$data));
}
}
return true;
} public static function destroy($PHPSESSID)
{
$sql = "delete from session where PHPSESSID = ?";
$sth = self::$handle->prepare($sql);
$sth->execute(array($PHPSESSID));
return true;
} public static function gc($lifetime)
{
$sql = "delete from session where update_time < ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array(self::$time-self::$lifetime));
return true;
}
} try{
$pdo = new PDO("mysql:host=localhost;dbname=test", "root" ,"");
}catch(PDOException $e)
{
echo $e->getMessage();
} session::start($pdo);

最新文章

  1. 苹果台式一体机笔记本安装win双系统攻略教程
  2. 如何开启telnet 23端口
  3. [LeetCode] Edit Distance(很好的DP)
  4. Mac OSX 快捷键&amp;命令行总览
  5. asp.net读取xml方法
  6. java CopyOnWriteArrayList的使用
  7. 【创建型】Singleton模式
  8. HOWTO Use Python in the web — Python v3.0.1 documentation
  9. SQL Server 已提交读快照 测试
  10. AsyncTask的新认识
  11. RabbitMQ 默认端口号
  12. Windows2003无法连接远程桌面问题 解决方法!
  13. Zookeeper~Linux环境下的部署
  14. Java线程池—ThreadPool简介
  15. ORA-00600: internal error code, arguments: [2662]
  16. GAN_李弘毅讲解
  17. libreoffice python 操作word及excel文档
  18. 031 分布式中,zookeeper的部署
  19. Hibernate一对多关联关系保存时的探究
  20. 升级win10,提示(RAM)内存不足2G的解决的方法,亲測可行

热门文章

  1. Z-Score数据标准化处理(python代码)
  2. HDU——1465不容易系列之一(错排公式)
  3. 第一个 XMLHttpRequest 例子(API)
  4. poj3311Hie with the Pie
  5. Java 微信公众号开发--- 接入微信
  6. 几个类和Table的方法
  7. java8 函数式接口——Function/Predict/Supplier/Consumer
  8. 快充 IC BQ25896 如何判斷 手機插著 adapter 充電器時,adapter Iout 大於限制,adapter Vout 小於 限制,導致 battery 不但沒充電且還需放電。
  9. Linux 之 FTP服务器搭建
  10. JSONP简单示例