Custom Session Manager in PHP

Source: Internet
Author: User

<?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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.