<?PHPclassCustomsessionImplementssessionhandlerinterface{Private $link; Private $lifetime; Public functionOpen$savePath,$session _name){ $this->lifetime=Get_cfg_var(' Session.gc_maxlifetime '); $this->link=Mysqli_connect(' localhost ', ' root ', ' root ', ' session_test '); Mysqli_query($this->link, "SET names UTF8"); if($this-link) { return true; } return false; } Public functionClose () {Mysqli_close($this-link); return true; } Public functionRead$session _id){ $sql= "Select *from sessions where Session_id= ' {$session _id} ' and Session_expires >". Time(); $result=Mysqli_query($this->link,$sql); if(mysqli_num_rows($result)){ return Mysqli_fetch_array($result) [' Session_data ']; } return""; } Public functionWrite$session _id,$session _data){ $newExp= Time()+$this-lifetime; //The first query is whether the specified session_id exists, and if it is the equivalent of the update data, the data is written $sql= "SELECT * from sessions where session_id={'$session _id‘}"; $result=Mysqli_query($this->link,$sql); if(mysqli_num_rows($result) ==1){ $sql= "UPDATE Sessions set session_expires= ' {$newExp} ', Session_data= ' {$session _data} ' where session_id= ' {$session _id}‘ "; }Else{ $sql= "INSERT into sessions values (' {$session _id}‘,‘$session _data‘,‘{$newExp}‘)"; } Mysqli_query($this->link,$sql); return mysqli_affected_rows($this->link) ==1; } Public functionDestroy$session _id){ $sql= "DELETE from sessions where Session_id= ' {$session _id}‘"; Mysqli_query($this->link,$sql); return mysqli_affected_rows($this->link) ==1; } Public functiongc$maxlifetime){ $sql= "DELETE from sessions where session_expires<". Time(); Mysqli_query($this->link,$sql); if(mysqli_affected_rows($this->link) >0){ return true; } return false; }}
<?PHPHeader("Content-type:text/html;charset=utf-8"); require_once' Customsession.php '; $customSession=Newcustomsession (); Ini_set(' Session.save_handler ', ' user '); Session_set_save_handler($customSession,true); Session_Start(); $_session[' Name ']= ' admin '; $_session[' pwd ']= ' 123456 ';
// destroy the session, delete the < from the database;? PHP require_once ' customsession.php '; $customSession=new customsession (); Ini_set (' Session.save_handler ', ' user '); Session_set_save_handler ($customSession,true); Session_Start (); Session_destroy ();
Custom Session Manager in PHP