PHP 使用MySQL管理Session的回调函数详解

前端之家收集整理的这篇文章主要介绍了PHP 使用MySQL管理Session的回调函数详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="56626" class="copybut" id="copybut56626" onclick="doCopy('code56626')"> 代码如下:

<div class="codebody" id="code56626">
<?PHP
class MySession extends DBsql {/
__construct()
/
public function construct() {
parent::
construct ();}/

open()

@param $sSavePath
@param $sSessionNames

@return true
*/
public function open($sSavePath,$sSessionName) {
return true;
}/
close()
/
public function close() {
return true;
}/

read()

@param $sSessionId
@return Session data or null on error...
/
public function read($sSessionId) {
$sSessionId = addslashes(trim($sSessionId));
$ssql = "SELECT
FROM tb_session WHERE F_SessionId = '{$sSessionId}'";
//echo $ssql;
if(false != ($result = $this->select($ssql))) {
//var_dump($result[0]['F_SessionData']);
return $result[0]['F_SessionData'];
} else {
return '';
}
}/
write()

@param $SessionId
@param $sSessionData

@return true or false
*/
public function write($sSessionId,$sSessionData) {
$arrSession = array();
$arrSession['F_SessionId'] = addslashes(trim($sSessionId));
$arrSession['F_SessionData'] = addslashes($sSessionData);
$arrSession['F_SessionCreated'] = date("Y-m-d H:i:s");
$arrSession['F_SessionUpdated'] = date("Y-m-d H:i:s",time() + 1440);// 新session及更新的session
if (false != ($result = $this->insertData('tb_session',$arrSession))) {
return true;
} else {
$arrUpdateSession = array();
$arrUpdateSession['F_SessionData'] = addslashes($sSessionData);
$arrUpdateSession['F_SessionUpdated'] = date("Y-m-d H:i:s",time() + 1440);
if (false != ($result = $this->updateData('tb_session',"'".addslashes($sSessionId)."'",'F_SessionId',$arrUpdateSession))) {
return true;
} else {
return false;
}
}
}/*
destroy()

@param $sSessionId
@return true
/
public function destroy($sSessionId) {
$sSessionId = addslashes(trim($sSessionId));
$this->delData($sSessionId,'tb_session');
return true;
}/

gc

@param $nMaxLifeTime seconds

@return true
/
public function gc($nMaxLifeTime) {
$dtExpiredTime = date("Y-m-d H:i:s",time() - $nMaxLifeTime);$ssql = "DELETE FROM tb_session WHERE F_SessionUpdated < {$dtExpiredTime}";
//echo $ssql;
$this->delete($ssql);return true;
}
/*
desctruct()

/
function
destruct() {
// ensure session data is written out before classes are destroyed
// (see http://bugs.PHP.net/bug.PHP?id=33772 for details)
@session_write_close();
} // __destruct
}
?>

猜你在找的PHP相关文章