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

一个用mysql内存表来代替php session的类

sessionexpiredtime = intval(ini_get(session.cache_expire))*10;//默认是180分钟,太长了,改为了30分钟 }catch(exception $exception){ $this->sessionexpiredtime = 1200; } try{ $this->sessioncookiename = ini_get(session.name); }catch(exception $exception){ $this->sessioncookiename = 'phpsessid'; } if(!isset($_cookie[$this->sessioncookiename])){ @session_start(); $this->sessionid=session_id(); }else{ $this->sessionid=$_cookie[$this->sessioncookiename]; } $classname = $enginename.sessionengine; $this->engine = new $classname( array( 'storage_name'=>$storage_name,//mysql table name or memcahce key which stores data; 'expire_time'=>$this->sessionexpiredtime, 'data_too_long_instead_value' => '{__data is *$* to long__}' ), $this->sessionid, &$enginebase ); $this->init(); $this->loadfromsession(); $this->engine->refresh(); $this->engine->cleanup(); } private function init() { $this->data = $this->engine->get(); if(empty($this->data)){ @session_start(); if(!empty($_session)){ $this->data = $_session; $this->engine->create(false, $this->data); } else { $this->engine->create(false, ); } } } public function loadfromsession($flagstartsession = false){ $flag=false; if($flagstartsession){ @session_start(); } if($_session&&is_array($_session)){ foreach($_session as $k=>$v){ if(!isset($this->data[$k])){ $this->data[$k] = $v; $flag=true; } } } if($flag){ $this->engine->set(false, $this->data); } } private function __get($nm) { if (isset($this->data[$nm])) { $r = $this->data[$nm]; return $r; } else { return null; } } private function __set($nm, $val) { $this->data[$nm] = $val; $this->engine->set(false, $this->data); } private function __isset($nm) { return isset($this->data[$nm]); } private function __unset($nm) { unset($this->data[$nm]); $this->engine->set(false, $this->data); } function __destruct(){ $this->data = null; $this->engine->close(); $this->engine = null; } } interface sessionengine { /* * set varibles * @param $arr array,array(varible name=>varible value,...) */ public function setvariable($arr); /* * get session value * @param $key string */ public function get($key=); /* * set session value * @param $key string * @param $value string */ public function set($key=,$value=); /* * set session value * @param $key string * @param $value string */ public function create($key=,$value=); /* * update the session's invalid time * @param $key string */ public function refresh($key=); /* * close mysql or memcache connection */ public function close(); /* * delete expired sessions */ public function cleanup(); } final class mysqlsessionengine implements sessionengine{ private $id=; private $storage_name='php_session'; private $storage_name_slow='php_session_slow'; private $data_too_long_instead_value = '{__data is ~ to long__}';//if data is longer than $max_session_data_length and you are using mysql 4 or below,insert this value into memery table instead. private $expire_time=1200; private $max_session_data_length = 2048; private $conn; private $mysql_version; public function mysqlsessionengine($arr=array(),$key=,&$_conn){ $this->setvariable($arr); $this->id = $key; if(empty($this->id)||strlen($this->id)!=32){ throw new exception(__file__.->.__line__.: session's cookie name can't be empty and it must have just 32 charactors!); } $this->conn = $_conn; if(!$this->conn||!is_resource($this->conn)){ throw new exception(__file__.->.__line__.: need a mysql connection!); } $this->mysql_version = $this->getone(select floor(version())); if($this->mysql_version $this->max_session_data_length = 255; } } public function setvariable($arr){ if(!empty($arr)&&is_array($arr)){ foreach($arr as $k=>$v){ $this->$k = $v; if($k=='storage_name'){ $this->storage_name_slow = $v.'_slow'; } } } } public function get($key=){ if($key==) $key = $this->id; $return = $this->getone('select value from '.$this->storage_name.' where id='.$key.''); if($return==$this->data_too_long_instead_value) { $return = $this->getone('select value from '.$this->storage_name_slow.' where id='.$key.''); } if(!$return) { $mysqlerror = mysql_error($this->conn); if(strpos($mysqlerror,doesn't exist)!==false) { $this->inittable(); } $return = array(); } else { $return = unserialize($return); } return $return; } public function close(){ @mysql_close($this->conn); } public function cleanup(){ if($this->mysql_version>4){ $sql = 'delete from '.$this->storage_name.' where date_add(`time`,interval '.$this->expire_time.' second) }else{ $sql = 'delete from '.$this->storage_name_slow.' where `time`+'.$this->expire_time.' if($_session['username']==leinchu){ echo $sql; } $this->execute($sql); $sql = 'delete from '.$this->storage_name.' where `time`+'.$this->expire_time.' if($_session['username']==leinchu){ echo $sql; } } $this->execute($sql); } public function refresh($key=){ if($this->mysql_version>4){ $sql = 'update '.$this->storage_name.' set `time`=current_timestamp() where id='.$key.''; }else{ $sql = 'update '.$this->storage_name.' set `time`=unix_timestamp() where id='.$key.''; } $return = $this->execute($sql); if(!$return){ $this->inittable(); $return = $this->execute($sql,true); } return $return; } public function create($key=,$value=){ if($key==) $key = $this->id; if($value != ) $value = mysql_real_escape_string(serialize($value),$this->conn); if(strlen($value)>$this->max_session_data_length) { if($this->mysql_version>4){ throw new exception(__file__.->.__line__.: session data is long than max allow length(.$this->max_session_data_length.)!); } } if($this->mysql_version>4){ $sql = 'replace into '.$this->storage_name.' set value=/''.$value.'/',id='.$key.',`time`=current_timestamp()'; }else{ $sql = 'replace into '.$this->storage_name.' set value=/''.$value.'/',id='.$key.',`time`=unix_timestamp()'; } $return = $this->execute($sql); if(!$return){ $this->inittable(); $return = $this->execute($sql,true); } return $return; } public function set($key=,$value=){ if($key==) $key = $this->id; if($value != ) $value = mysql_real_escape_string(serialize($value),$this->conn); $sql = 'update '.$this->storage_name.' set value=/''.$value.'/' where id='.$key.''; if(strlen($value)>$this->max_session_data_length) { if($this->mysql_version>4){ throw new exception(__file__.->.__line__.: session data is long than max allow length(.$this->max_session_data_length.)!); } $sql = 'replace into '.$this->storage_name_slow.' set value=/''.$value.'/',id='.$key.',`time`=unix_timestamp()'; $this->execute($sql,true); $sql = 'update '.$this->storage_name.' set value=/''.$this->data_too_long_instead_value.'/' where id='.$key.''; } $return = $this->execute($sql); if(!$return){ $this->inittable(); $return = $this->execute($sql,true); } return $return; } private function inittable(){ if($this->mysql_version>4){ $sql = create table if not exists `.$this->storage_name.` ( `id` char(32) not null default 'err', `value` varbinary(.$this->max_session_data_length.) null, `time` timestamp not null default current_timestamp on update current_timestamp, primary key (`id`), key `time` (`time`) ) engine=memory; ; }else{ $sqlslow = create table if not exists `.$this->storage_name._slow` ( `id` char(32) not null default 'err', `value` text null, `time` int(10) not null default '0', primary key (`id`), key `time` (`time`) ) engine=myisam; ; $this->execute($sqlslow,true); $sql = create table if not exists `.$this->storage_name.` ( `id` char(32) not null default 'err', `value` varchar(255) null, `time` int(10) not null default '0', primary key (`id`), key `time` (`time`) ) engine=memory; ; } return $this->execute($sql,true); } private function execute($sql,$die=false) { if($die) { mysql_query($sql,$this->conn) or die(exe sql error:
.mysql_error().
.$sql.); } else { mysql_query($sql,$this->conn); if(mysql_error()){ return false; }else{ return true; } } } private function getone($sql,$die=false){ $rs = $this->query($sql,$die); if($rs && ($one = mysql_fetch_row($rs)) ){ return $one[0]; }else{ return false; } } private function query($sql,$die=false){ if($die) $rs = mysql_query($sql,$this->conn) or die(query sql error:
.mysql_error().
.$sql.); else $rs = mysql_query($sql,$this->conn); return $rs; } } $lnk = mysql_connect('localhost', 'root', '123456') or die ('not connected : ' . mysql_error()); // make foo the current db mysql_select_db('test', $lnk) or die ('can/'t use foo : ' . mysql_error()); $s = new session($lnk); if(!$s->last){ $s->last = time(); } echo first visit at .$s->last.
; if(!$s->lastv){ $s->lastv = 0; } $s->lastv++; echo lastv=.$s->lastv.
; echo test=.$s->test.
; if(isset($_get['max'])){ $s->boom = str_repeat(ok,255); } if(isset($_get['boom'])){ $s->boom = $_get['boom']; } echo boom=.$s->boom.
; ?>
复制代码
其它类似信息

推荐信息