PHP独立Session数据库存储操作类分享
直接上代码:
classDbSession {
constTYPE_INT=1; constTYPE_STR=2;
/** *Databaseconfigration * *@vararray */ private$_config=array( ‘host'=>'127.0.0.1′, ‘port'=>3306, ‘username'=>‘root', ‘password'=>‘root', ‘dbname'=>‘db_mylab', ‘tablename'=>‘t_sessions', ‘cookie_prefix'=>‘mylab_', ‘cookiepath'=>‘/', ‘cookiedomain'=>”, ‘cookie_timeout'=>900 );
/** *Tablefieldstypearray * *@vararray */ private $_db_fields=array( ‘crc32sid' =>self::TYPE_INT, ‘sessionhash' =>self::TYPE_STR, ‘idhash' =>self::TYPE_STR, ‘userid' =>self::TYPE_INT, ‘ipaddress' =>self::TYPE_STR, ‘lastactivity' =>self::TYPE_STR, ‘location' =>self::TYPE_STR, ‘loggedin' =>self::TYPE_INT, ‘heartbeat' =>self::TYPE_STR );
/** *dbobj * *@varmysqliobject */ private$_mysqli=null;
/** *Weatherthesessionwascreatedorexistedpreviously * *@varbool */ private$_created=false;
/** *Arrayofchanges. * *@vararray */ private$_changes=array();
/** *@varbool */
private$_db_inited=false;
/** *sessionhost * *@varstring */ private$_session_host=”;
/** *sessionidhash * *@varstring */ private$_session_idhash=”;
private$_dbsessionhash=”;
private$_vars=array();
publicfunction__construct() { $this->_dbsessionhash=addslashes($this->get_cookie(‘sessionhash'));
$this->_session_host=substr($_SERVER[‘REMOTE_ADDR'],0,15);
#Thisshould*never*changeduringasession $this->_session_idhash=md5($_SERVER[‘HTTP_USER_AGENT'].self::fetch_substr_ip(self::fetch_alt_ip()));
$this->_init_config(); $this->init_db();
$gotsession=false;
if($this->_dbsessionhash) { $sql=‘ SELECT* FROM‘.$this->_config[‘tablename'].‘ WHERE crc32sid=‘.sprintf(‘%u',crc32($this->_dbsessionhash)).‘ ANDsessionhash='‘.$this->_dbsessionhash.‘' ANDidhash='‘.$this->_session_idhash.‘' ANDheartbeat>'‘.date(‘Y-m-dH:i:s',TIMENOW–$this->_config[‘cookie_timeout']).‘'‘; //echo$sql;exit; $result=$this->_mysqli->query($sql); $session=$result->fetch_array(MYSQLI_ASSOC);
if($sessionAND($this->fetch_substr_ip($session[‘ipaddress'])==$this->fetch_substr_ip($this->_session_host))) { $gotsession=true; $this->_vars=$session; $this->_created=false; } }
if($gotsession==false) { $this->_vars=$this->fetch_session(); $this->_created=true; $gotsession=true; }
if($this->_created==false) { $this->set(‘lastactivity',date(‘Y-m-dH:i:s',TIMENOW)); $this->set(‘location',$_SERVER[‘REQUEST_URI']); }
}
/** *Buildsanarraythatcanbeusedtobuildaquerytoinsert/updatethesession * *@return array Arrayofcolumnname=>preparedvalue */ privatefunction_build_query_array() { $return=array(); foreach($this->_db_fieldsAS$fieldname=>$cleantype) { switch($cleantype) { caseself::TYPE_INT: $cleaned=is_numeric($this->_vars["$fieldname"])?$this->_vars["$fieldname"]:intval($this->_vars["$fieldname"]); break; caseself::TYPE_STR: default: $cleaned="'".addslashes($this->_vars["$fieldname"])."'"; } $return["$fieldname"]=$cleaned; }
return$return; }
/** *Setsasessionvariableandupdatesthechangelist. * *@param string Nameofsessionvariabletoupdate *@param string Valuetoupdateitwith */ publicfunctionset($key,$value) { if($this->_vars["$key"]!=$value) { $this->_vars["$key"]=$value; $this->_changes["$key"]=true; } }
publicfunctionget($key) { return$this->_vars["$key"]; }
publicfunctionunsetchangedvar($var) { if(isset($this->_changes["$var"])) { unset($this->_changes["$var"]); } }
/** *Fetchesavalidsessionhashvalue,notnecessarilytheonetiedtothissession. * *@return string 32-charactersessionhash */ staticfunctionfetch_sessionhash() { returnhash(‘md5′,TIMENOW.rand(1,100000).uniqid()); }
privatefunction_init_config() { $registry=Zend_Registry::getInstance(); $config=$registry->get(‘config'); $this->_config[‘host']=$config->database->params->host; $this->_config[‘port']=$config->database->params->port; $this->_config[‘username']=$config->database->params->username; $this->_config[‘password']=$config->database->params->password; $this->_config[‘dbname']=$config->database->params->dbname; $this->_config[‘tablename']=$config->database->session->tablename;
}
/** *initializedatabaseconnection */ publicfunctioninit_db() { if($this->_db_inited) { returntrue; }
//mysqli_report(MYSQLI_REPORT_OFF);
$this->_mysqli=newmysqli( $this->_config[‘host'], $this->_config[‘username'], $this->_config[‘password'], $this->_config[‘dbname'], $this->_config[‘port'] );
/*checkconnection*/ if(mysqli_connect_errno()) {
//printf("Connectfailed:%sn",mysqli_connect_error()); //echo‘in‘,__FILE__,‘online‘,__LINE__; echo"{success:false,errors:{reason:‘Connectfailed:".addslashes(mysqli_connect_error())."'}}"; exit();
}
$this->_mysqli->query(‘setnameslatin1′); $this->_db_inited=true;
returntrue; }
/** *FetchesanalternateIPaddressofthecurrentvisitor,attemptingtodetectproxiesetc. * *@return string */ staticfunctionfetch_alt_ip() { $alt_ip=$_SERVER[‘REMOTE_ADDR'];
if(isset($_SERVER[‘HTTP_CLIENT_IP'])) { $alt_ip=$_SERVER[‘HTTP_CLIENT_IP']; } elseif(isset($_SERVER[‘HTTP_FROM'])) { $alt_ip=$_SERVER[‘HTTP_FROM']; }
return$alt_ip; }
/** *ReturnstheIPaddresswiththespecifiednumberofoctetsremoved * *@param string IPaddress * *@return string truncatedIPaddress */ staticfunctionfetch_substr_ip($ip,$length=null) { returnimplode(‘.',array_slice(explode(‘.',$ip),0,4–$length)); }
/** *Fetchesadefaultsession.Usedwhencreatinganewsession. * *@param integer UserIDthesessionshouldbefor * *@return array Arrayofsessionvariables */ publicfunctionfetch_session($userid=0) { $sessionhash=self::fetch_sessionhash();
$this->set_cookie(‘sessionhash',$sessionhash);
returnarray( ‘crc32sid' =>sprintf(‘%u',crc32($sessionhash)), ‘sessionhash' =>$sessionhash, ‘idhash' =>$this->_session_idhash, ‘userid' =>$userid, ‘ipaddress' =>$this->_session_host, ‘lastactivity' =>date(‘Y-m-dH:i:s',TIMENOW), ‘location' =>$_SERVER[‘REQUEST_URI'], ‘loggedin' =>$userid?1:0, ‘heartbeat' =>date(‘Y-m-dH:i:s',TIMENOW) ); }
publicfunctionget_cookie($cookiename) { $full_cookiename=$this->_config[‘cookie_prefix'].$cookiename;
if(isset($_COOKIE[$full_cookiename])) { return$_COOKIE[$full_cookiename]; } else { return false; } }
publicfunctionset_cookie($name,$value=”,$permanent=1,$allowsecure=true) {
if($permanent) { $expire=TIMENOW+60*60*24*365; } else { $expire=0; }
if($_SERVER[‘SERVER_PORT']=='443′) { //we'reusingSSL $secure=1; } else { $secure=0; }
//checkforSSL $secure=((REQ_PROTOCOL===‘https'AND$allowsecure)?true:false);
$name=$this->_config[‘cookie_prefix'].$name; $filename=‘N/A'; $linenum=0;
if(!headers_sent($filename,$linenum)) {//considershowinganerrormessageiftherenotsentusingabovevariables? if($value==”ANDstrlen($this->_config[‘cookiepath'])>1ANDstrpos($this->_config[‘cookiepath'],‘/')!==false) { //thiswillattempttounsetthecookieateachdirectoryupthepath. //ie,cookiepath=/test/abc/.Thesewillbeunset:/,/test,/test/,/test/abc,/test/abc/ //Thisshouldhopefullypreventcookieconflictswhenthecookiepathischanged. $dirarray=explode(‘/',preg_replace(‘#/+$#',”,$this->_config[‘cookiepath'])); $alldirs=”; foreach($dirarrayAS$thisdir) { $alldirs.="$thisdir"; if(!empty($thisdir)) {//tryunsettingwithoutthe/attheend setcookie($name,$value,$expire,$alldirs,$this->_config[‘cookiedomain'],$secure); } $alldirs.="/"; setcookie($name,$value,$expire,$alldirs,$this->_config[‘cookiedomain'],$secure); } } else { setcookie($name,$value,$expire,$this->_config[‘cookiepath'],$this->_config[‘cookiedomain'],$secure); } } elseif(!DEBUG) { echo"can'tsetcookies"; } }
privatefunction_save() { $cleaned=$this->_build_query_array();
if($this->_created) { //var_dump($cleaned); #insertquery $this->_mysqli->query(‘ INSERTIGNOREINTO‘.$this->_config[‘tablename'].‘ (‘.implode(‘,',array_keys($cleaned)).‘) VALUES (‘.implode(‘,',$cleaned).‘) ‘); } else { #updatequery $update=array();
foreach($cleanedAS$key=>$value) { if(!empty($this->_changes["$key"])) { $update[]="$key=$value"; } }
if(sizeof($update)>0) { $sql=‘UPDATE‘.$this->_config[‘tablename'].‘ SET‘.implode(‘,‘,$update).‘ WHEREcrc32sid=‘.sprintf(‘%u',crc32($this->_dbsessionhash)).‘ ANDsessionhash='‘.$this->_dbsessionhash.‘'‘; //echo$sql; $this->_mysqli->query($sql); } } }
publicfunctiongetOnlineUserNum() { $sql=‘ SELECTcount(*)ascnt FROM‘.$this->_config[‘tablename'].‘ WHEREloggedin=1 ANDheartbeat>'‘.date(‘Y-m-dH:i:s',TIMENOW–$this->_config[‘cookie_timeout']).‘'‘;
$result=$this->_mysqli->query($sql); $row=$result->fetch_array(MYSQLI_ASSOC); return$row[‘cnt']; }
privatefunction_gc() { $rand_num=rand();#randowintegerbetween0andgetrandmax() if($rand_num<100) { $sql=‘DELETEFROM‘.$this->_config[‘tablename'].‘ WHEREheartbeat<'‘.date(‘Y-m-dH:i:s',TIMENOW–$this->_config[‘cookie_timeout']).‘'‘;
$this->_mysqli->query($sql); } }
publicfunction__destruct() { $this->_save(); $this->_gc(); $this->_mysqli->close(); }
}