SESSION存放在数据库用法实例

前端之家收集整理的这篇文章主要介绍了SESSION存放在数据库用法实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了SESSION存放在数据库用法分享给大家供大家参考。具体如下:

PHP;"> 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"),"read"),"write"),"destroy"),"gc")); session_start(); // etc... ?>

希望本文所述对大家的PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/21312.html

猜你在找的PHP相关文章