<?php if(!defined(‘Sys_root‘)) exit(‘NOT Pathinfo,无路径信息‘);
/*
*简单的session数据库类
*session自定义文件,主要用于用户登录
*/
class dbsession
{
public $dbsql;//数据库对象
public $config;//数据库配置文件
public function __construct($uid = ‘useid‘){
global $dbsql,$config;
$this->dbsql = & $dbsql;
$this->config = & $config;
session_set_save_handler(array($this,‘mysql_session_open‘),array($this,‘mysql_session_close‘),array($this,‘mysql_session_read‘),array($this,‘mysql_session_write‘),array($this,‘mysql_session_destory‘),array($this,‘mysql_session_des_all‘));
register_shutdown_function(‘session_write_close‘);
session_name($uid);
}
public function mysql_session_open($session_path = ‘‘,$session_name = ‘‘){
//global $dbsql,$config;
if(!$this->dbsql->get_is_link()){
$this->dbsql->open(false,$this->config);
}
}
public function mysql_session_close(){
//global $dbsql;
$this->dbsql->destory_result();
}
public function mysql_session_read($sid){
//global $dbsql;
$sid = session_id();
if(empty($sid)){
return false;
}else{
$data = ‘value‘;
$where = ‘sid=\‘‘.$sid.‘\‘‘;
$value = $this->dbsql->read($data,‘uselogin‘,$where);
}
return stripslashes($value[0][‘value‘]);
}
public function mysql_session_write($sid,$sess_data){
//global $dbsql;
$value = $sess_data;
$time = time();
$data = array(‘sid‘=>$sid,‘time‘=>$time,‘value‘=>$value);
$this->dbsql->in_data($data,‘uselogin‘,false,true);
}
public function mysql_session_destory($sid){
//global $dbsql;
$where = ‘sid=\‘‘.$sid.‘\‘‘;
unset($_SESSION[‘name‘]);//消除session变量
$cookiename = session_name();
setcookie($cookiename,‘‘,time()-86400,‘/‘);//消除客服端COOkie
return $this->dbsql->de_data(‘uselogin‘,$where);
}
public function mysql_session_des_all(){
//global $dbsql;
$maxlefttime = ini_get(‘session.gc_maxlifetime‘);
//$where = ‘time < \‘‘.time()+$maxlefttime.‘\‘‘;
$where = ‘time < 1388000000‘;
$v = $this->dbsql->de_data(‘uselogin‘,$where);
$this->dbsql->destory_result();
return $v;
}
}
//$dbsession =new dbsession;
?>
PHP手册上有很好的例子