PHP自定义MySQL Session处理类实现详解
以下是使用PHP实现的自定义Session管理类代码示例:
<?php
class MySession extends DBSQL {
/**
/**
* 构造函数
*/
*/
public function __construct() {
parent::__construct();
}
/**
/**
* 打开Session连接
*/
*
/**
* @param string $sSavePath
* @param string $sSessionName
* @return boolean true
*/
<string> $sSavePath
/**
* @param string $sSessionName
*/ <string>$sSessionNames
*
/**
* @return boolean true
*/ <boolean> true
*/
public function open($sSavePath, $sSessionName) {
return true;
}
/**
/**
* 关闭Session连接
*/
*/
public function close() {
return true;
}
/**
/**
* 读取Session数据
*/
*
/**
* @param string $sSessionId
* @return string Session data or null on error...
*/ <string> $sSessionId
/**
* @return string Session data or null on error...
*/ <string> 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 '';
}
}
/**
/**
* 写入Session数据
*/
*
/**
* @param string $SessionId
* @param string $sSessionData
* @return boolean true or false
*/ <string> $SessionId
/**
* @param string $sSessionData
*/ <string> $sSessionData
*
/**
* @return boolean true or false
*/ <boolean> 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;
}
}
}
/**
/**
* 销毁Session
*/
*
/**
* @param string $sSessionId
* @return boolean true
*/ <string> $sSessionId
/**
* @return boolean true
*/ <boolean> true
*/
public function destroy($sSessionId) {
$sSessionId = addslashes(trim($sSessionId));
$this->delData($sSessionId, 'F_SessionId', 'tb_session');
return true;
}
/**
/**
* 垃圾回收(GC)
*/
*
/**
* @param int $nMaxLifeTime seconds
* @return boolean true
*/ <int> $nMaxLifeTime seconds
*
/**
* @return boolean true
*/ <boolean> 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;
}
/**
/**
* 析构函数
*/
*
*/
function __destruct() {
// 确保在类销毁前将Session数据写入磁盘
// (详见 http://bugs.php.net/bug.php?id=33772)
// (see http://bugs.php.net/bug.php?id=33772 for details)
@session_write_close();
} // __destruct
}
?>