您好,登錄后才能下訂單哦!
怎么將SESSION存放在數據庫中?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
具體如下:
<?php /* CREATE TABLE `ws_sessions` ( `session_id` varchar(255) binary NOT NULL default '', `session_expires` int(10) unsigned NOT NULL default '0', `session_data` text, PRIMARY KEY (`session_id`) ) TYPE=InnoDB; */ class session { // session-lifetime var $lifeTime; // mysql-handle var $dbHandle; function open($savePath, $sessName) { // get session-lifetime $this->lifeTime = get_cfg_var("session.gc_maxlifetime"); // open database-connection $dbHandle = @mysql_connect("localhost","root",""); $dbSel = @mysql_select_db("test",$dbHandle); // return success if(!$dbHandle || !$dbSel) return false; $this->dbHandle = $dbHandle; return true; } function close() { $this->gc(ini_get('session.gc_maxlifetime')); // close database-connection return @mysql_close($this->dbHandle); } function read($sessID) { // fetch session-data $res = mysql_query("SELECT session_data AS d FROM ws_sessions WHERE session_id = '$sessID' AND session_expires > ".time(),$this->dbHandle); // return data or an empty string at failure if($row = mysql_fetch_assoc($res)) return $row['d']; return ""; } function write($sessID,$sessData) { // new session-expire-time $newExp = time() + $this->lifeTime; // is a session with this id in the database? $res = mysql_query("SELECT * FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle); // if yes, if(mysql_num_rows($res)) { // ...update session-data mysql_query("UPDATE ws_sessions SET session_expires = '$newExp', session_data = '$sessData' WHERE session_id = '$sessID'",$this->dbHandle); // if something happened, return true if(mysql_affected_rows($this->dbHandle)) return true; } // if no session-data was found, else { // create a new row mysql_query("INSERT INTO ws_sessions ( session_id, session_expires, session_data) VALUES( '$sessID', '$newExp', '$sessData')",$this->dbHandle); // if row was created, return true if(mysql_affected_rows($this->dbHandle)) return true; } // an unknown error occured return false; } function destroy($sessID) { // delete session-data mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle); // if session was deleted, return true, if(mysql_affected_rows($this->dbHandle)) return true; // ...else return false return false; } function gc($sessMaxLifeTime) { // delete old sessions mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle); // return affected rows return mysql_affected_rows($this->dbHandle); } } $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(); // etc... ?>
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。