您好,欢迎访问一九零五行业门户网

修改Session存放方式为MySQL的类

lt;?php/*** 修改session存放方式为mysql aboc qq:9986584*/class session{//过期时间private $_left_time = 1440;public funct
time();
$row = dmmysql::open()->fetchrow( $sql );
return $row['data'];
}
/**
   * 写
   */
public function write( $sessid , $sessdata ) {
$data = array(
    'expiry'    =>   time()+ $this->_left_time,
    'data'      =>   $sessdata,
    'ip'   =>   '192.168.1.123'
);
if( dmmysql::open()->fetchrow(select sessid from dm_session where sessid ='$sessid') ) {
   //更新
   $where = sessid = '$sessid';
   if( dmmysql::open()->update( 'dm_session',$data,$where ) ){
    return true;
   } else {
    return false;
   }
} else {
   //插入
   $data['sessid'] = $sessid;
   if( dmmysql::open()->insert('dm_session',$data) ){
    return true;
   } else {
    return false;
   }  
}
}
/**
   * 指定销毁
   */
public function destroy( $sessid ) {
$where = sessid = '$sessid';
if(dmmysql::open()->delete('dm_session',$where)) {
   return true;
} else {
   return false;
}
}
/**
   * 销毁过期的数据
   */
public function gc( $maxlifetime ) {
//随机销毁数据,减轻服务器压力
if( rand(0,3) == 3 ) {
   $where = expiry    if( dmmysql::open()->delete('dm_session',$where) ) {
      return true;
   } else {
    return false;
   }
}
}
}
$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();
?>
在每个使用session的文件前include一下就行了
数据库:
create table `dm_session` (
   `sessid` char(32) not null default '',
   `expiry` int(10) not null default '0',
   `data` text not null,
   `ip` char(15) not null default '',
   primary key   (`sessid`),
   key `sesskid` (`sessid`,`expiry`)
) engine=myisam default charset=gbk;

其它类似信息

推荐信息