PHP Session Save Database

Source: Internet
Author: User
Tags array php session return sessions string

This sesison to the database is very simple, is based on the session_id of the database crud operations, mainly used, Session_set_save_handler this method, custom session execution method,

First create the data table

CREATE TABLE ' Sessions ' (' session_id ' varchar (255) NOT NULL, ' session_expires ' int (one) DEFAULT null, ' Session_data ' text, PRIMARY KEY (' session_id ')) Engine=innodb DEFAULT Charset=utf8

Then encapsulate the tool class that operates the session:

 -->LifeTime = Get_cfg_var ("Session.gc_maxlifetime");
       $dbHandle = mysql_connect ("localhost", "root", "root");
       
       $dbSel = mysql_select_db ("mysession", $dbHandle);
       if (! $dbHandle! $dbSel) return false;
       $this->dbhandle = $dbHandle;
   return true;
      
       function Close () {$this->GC (ini_get (' session.gc_maxlifetime '));
   Return @mysql_close ($this->dbhandle);
                           function Read ($sessID) {$res = mysql_query ("Select Session_data as D from sessions
       
       WHERE session_id = ' $sessID ' and Session_expires >. Time (), $this->dbhandle);
       if ($row = Mysql_fetch_assoc ($res)) return $row [' d '];
   Return "";
       
       function Write ($sessID, $sessData) {$newExp = time () + $this->lifetime; $res = mysql_query ("SELECT * from sessions WHERE session_id = ' $sessID '", $tHis->dbhandle); if ($res) {mysql_query ("UPDATE sessions SET session_expires = ' {$newExp} ', Session_data = ' {$se
				
           
           Ssdata} ' WHERE session_id = ' {$sessID} ', $this->dbhandle);
       if (Mysql_affected_rows ($this->dbhandle)) return true;
                         else {mysql_query ("INSERT into sessions session_id,
                         Session_expires, Session_data) VALUES (
           
           ' {$sessID} ', ' {$newExp} ', ' {$sessData} ', $this->dbhandle);
       if (Mysql_affected_rows ($this->dbhandle)) return true;
   return false; The function Destroy ($sessID) {mysql_query ("DELETE from sessions WHERE session_id = ' $sessID '", $this->
      
       Dbhandle);
          if (Mysql_affected_rows ($this->dbhandle)) return true;
   return false; The function gc ($sessMaxLifeTime) {mysql_query ("DELETE from sessions WHERE Session_expires <").
       
       $this->dbhandle);
   Return Mysql_affected_rows ($this->dbhandle); } #对session进行测试, the discovery database does not deposit data only session_id, and session_expires values, in fact session_data is there is only we do not see   $session = new
	Session (); Session_set_save_handler Array (& $session, "open"), Array (& $session, "Close"), Array (& $session, "R
			EAD "), Array (& $session," write "), Array (& $session," destroy "), Array (& $session," GC "));
			Session_Start ();	
			$session->write (session_id (), Json_encode (Array ("name" => "Gxx", "Pass" => "123"));
 echo $session->read (session_id ());?> doesn't support illustrations here.
Database data:

S430j9t480ocbovq6a7a0rlk22 1435054078

Session Query data:
Json
    • Name "Gxx"
    • Pass "123" Don't be blinded by things ....



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.