码迷,mamicode.com
首页 > Web开发 > 详细

php的session可以自定义处理机制哦

时间:2016-05-18 00:21:01      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

<?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手册上有很好的例子

 

php的session可以自定义处理机制哦

标签:

原文地址:http://www.cnblogs.com/phplhs/p/5503574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!