欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php session存数据库_PHP教程

程序员文章站 2022-04-20 08:36:54
...

php session存数据库

这个sesison存到数据库很简单,就是根据session_id进行对数据库的CRUD操作,主要是用到了,session_set_save_handler这个方法,自定义session的执行方法,

首先创建数据表


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

然后封装操作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 = '{$sessData}' 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;
   }
   function destroy($sessID) {
       
       mysql_query("DELETE FROM sessions WHERE session_id = '$sessID'",$this->dbHandle);
      
       if(mysql_affected_rows($this->dbHandle))
           return true;
       
       return false;
   }
   function gc($sessMaxLifeTime) {
       
       mysql_query("DELETE FROM sessions WHERE session_expires 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());
?>  
这里居然不支持插图。。
数据库数据:

s430j9t480ocbovq6a7a0rlk22 1435054078

session查询数据:
JSON
  • name"gxx"
  • pass"123"不要被事物蒙蔽了眼睛。。。。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1022402.htmlTechArticlephp session存数据库 这个sesison存到数据库很简单,就是根据session_id进行对数据库的CRUD操作,主要是用到了,session_set_save_handler这个方法,自...
相关标签: 数据库