php session数据库类
配制
<?php
/配制/
$config=array(
/ 数据库设置 /
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'php', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123', // 密码
'DB_PREFIX' => 'jiaodu_', // 数据库表前缀
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
/ SESSION设置 /
'SESSION_START' => 'user', //session方式,文件方式:file, 数据库设置为user
<?php
/**
* session类
* @author 角度 QQ:1286522207
*
*/
class session extends Action{
private $session_db;
private $session_Overdue;
private $db_tablename;//数据库名称
function __construct(){
$this->session_Overdue=3600;
$this->session_db= $this->db();
}
/**
* 打开
*/
public function open() {
return true;
}
/**
* 关闭
*/
public function close() {
return true;
}
/**
* 读取
*/
public function read($id) {
$where['id']=$id;
$this->session_db->M('session');
$value=$this->session_db->find($where);
if ($value) {
return $value['data'];
} else {
return "";
}
}
/**
* 写入
*/
public function write($id, $datas) {
$where['id']=$id;
$date['data']=$datas;
$date['time']=time()+$this->session_Overdue;
$date['ip']=$this->session_db->get_client_ip();
if (empty($_SESSION['user_id'])){
$date['user']=0;
}else {
$date['user']=$_SESSION['user_id'];
}
$this->session_db->M('session');
$up=$this->session_db->Update($date, $where);
if ($up){
return true;
}else {
$date['id']=$id;
$this->session_db->Insert($date);
return true;
}
}
/**
* 摧毁
*/
public function destroy($id) {
$where['id']=$id;
return $this->session_db->Delete($where);
}
/**
* 回收
*/
public function gc($max) {
return true;
}
function __destruct(){
$this->session_db->Delete("`time`<'".time()."'");
}
}
ini_set ( 'session.save_handler',$config['SESSION_START']);
$session = new session ();
session_set_save_handler ( array ($session, 'open' ), array ($session, 'close' ), array ($session, 'read' ), array ($session, 'write' ), array ($session, 'destroy' ), array ($session, 'gc' ) );
session_start();
//该片段来自于http://outofmemory.cn






