Home  >  Article  >  Backend Development  >  php session save database_PHP tutorial

php session save database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:48:371019browse

php session storage database

It is very simple to save this session to the database. It is to perform CRUD operations on the database based on the session_id. It mainly uses the session_set_save_handler method to customize the execution method of the session.

First create the data table


CREATE TABLE `sessions` ( `session_id` varchar(255) NOT NULL, `session_expires` int(11) DEFAULT NULL, `session_data` text, PRIMARY KEY (`session_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

Then encapsulate the tool class for operating the session:
<!--?php
//error_reporting(0);
class session {
  
   var $lifeTime;
  
   var $dbHandle;
   function open($savePath, $sessName) {
       
       $this--->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(&#39;session.gc_maxlifetime&#39;));
      
       return @mysql_close($this->dbHandle);
   }
   function read($sessID) {
       
       $res = mysql_query("SELECT session_data AS d FROM sessions
                           WHERE session_id = &#39;$sessID&#39;
                           AND session_expires > ".time(),$this->dbHandle);
       
       if($row = mysql_fetch_assoc($res))
           return $row[&#39;d&#39;];
       return "";
   }
   function write($sessID,$sessData) {
	   
	
       
       $newExp = time() + $this->lifeTime;
       
       $res = mysql_query("SELECT * FROM sessions
                           WHERE session_id = &#39;$sessID&#39;",$this->dbHandle);
      
	   
       if($res) {
          
		   
           mysql_query("UPDATE sessions SET session_expires = &#39;{$newExp}&#39;,session_data = &#39;{$sessData}&#39; WHERE session_id = &#39;{$sessID}&#39;",$this->dbHandle);
				
           
           if(mysql_affected_rows($this->dbHandle))
               return true;
       }
       
       else {
         
           mysql_query("INSERT INTO sessions (
                         session_id,
                         session_expires,
                         session_data)
                         VALUES(
                         &#39;{$sessID}&#39;,
						 &#39;{$newExp}&#39;,
						 &#39;{$sessData}&#39;)",$this->dbHandle);
           
           if(mysql_affected_rows($this->dbHandle))
               return true;
       }
       
       return false;
   }
   function destroy($sessID) {
       
       mysql_query("DELETE FROM sessions WHERE session_id = &#39;$sessID&#39;",$this->dbHandle);
      
       if(mysql_affected_rows($this->dbHandle))
           return true;
       
       return false;
   }
   function gc($sessMaxLifeTime) {
       
       mysql_query("DELETE FROM sessions WHERE session_expires < ".time(),$this->dbHandle);
       
       return mysql_affected_rows($this->dbHandle);
   }
}
#对session进行测试,发现数据库中并没有存入数据只有session_id,和session_expires的数值,其实session_data是存在的只是我们看不到
 $session = new session();
	session_set_save_handler(array(&$session,"open"),
							 array(&$session,"close"),
							 array(&$session,"read"),
							 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());
?>  
这里居然不支持插图。。
Database data:

s430j9t480ocbovq6a7a0rlk22 1435054078

Session query data:
JSON
  • name"gxx"
  • pass"123"Don't be blinded by things. . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022402.htmlTechArticlephp session storage database It is very simple to save the session to the database, which is to perform CRUD operations on the database based on the session_id, mainly Used, session_set_save_handler method, since...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn