php session存資料庫

來源:互聯網
上載者:User

標籤:

這個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的工具類:
<?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('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 < ".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());?>  這裡居然不支援插圖。。
資料庫資料:

s430j9t480ocbovq6a7a0rlk22    1435054078    

session查詢資料:
JSON
  • name"gxx"
  • pass"123"
不要被事物蒙蔽了眼睛。。。。

php session存資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.