冲动下发个SessionHandle[数据库实现]

1 view
Skip to first unread message

SysTem128

unread,
Dec 15, 2007, 10:23:09 AM12/15/07
to cnPhpDevelop
<?php
/**
* session处理器
* iRecoder为数据库组件,自己完成:)
* @author MoXie SysT...@GMail.Com
*/
class SessionHandler extends iRecoder
{
public $_sessionId; # session 编号
public $_sessionSn; # session 定位序列符
public $_sessionNumber; # session 定位计量值
public $_sessionPosition; # session 定位值
public $_sessionExpire; # session 过期时间
public $_sessionIsQuery; # session 请求状态
public function __construct()
{
parent::__construct(); #连接数据库
}
/**
* 用于array_reduce组合使用
*/
public function reduce($a,$b)
{
return $a.$b;
}
/**
* 获取定位序列符
*
* @return unknown
*/
public function getSessionSn()
{
$sessionId = $this->_sessionId;
$sessionSnList = array();
$sessionString = array();
preg_match_all('/([a-zA-Z0-9]{1})/',$sessionId,$sessionSnList);
$sessionSnList = current($sessionSnList);
foreach ($sessionSnList as $key => $value)
{
$sessionString[] = ord($value);
}
$sessionString = array_reduce($sessionString,array($this,"reduce"));
$this->_sessionSn = $sessionString;
return $sessionString;
}
/**
* 获取定位计量值
*
*/
public function getSessionNumber()
{
$sessionSn = $this->_sessionSn;
$sSnLength = strlen($sessionSn);
$sessionNumber = 0;
preg_match_all('/([0-9]{1})/',$sessionSn,$sessionSnList);
$sessionSnList = current($sessionSnList);
foreach ($sessionSnList as $key => $value)
{
$sessionNumber += $value;
}
$this->_sessionNumber = (int)$sessionNumber;
$this->_sessionSn = strlen($sessionSn);
return $sessionNumber;
}

/**
* 获取所有定位字段
*
*/
public function getSessionPosition()
{
$sessionId = $this->_sessionId;
$this->getSessionSn();
$this->getSessionNumber();
$sessionSn = $this->_sessionSn;
$sessionNumber = $this->_sessionNumber;
$sessionPosition = array();
#
$sessionPosition['sId'] = $sessionPosition[0] = $sessionId;
$sessionPosition['sSn'] = $sessionPosition[1] = $sessionSn;
$sessionPosition['sNu'] = $sessionPosition[2] = $sessionNumber;
$this->_sessionPosition = $sessionPosition;
return $sessionPosition;
}
/**
* 读取 Session
*
* @return unknown
*/
public function selectSession()
{
# 修改语句
$selectSQL = 'select ';
$selectSQL .= '`Session_Id`,`Session_sNu`,`Session_sSn`,`Session_sId`,`Session_sData`,`Session_ExpireTime` ';
$selectSQL .= 'FROM `t_session_handler` WHERE 1=1 '; #留空格
$selectSQL .= 'AND `Session_sSn`=:Session_sSn ';
$selectSQL .= 'AND `Session_sNu`=:Session_sNu ';
$selectSQL .= 'AND `Session_sId`=:Session_sId ';
$selectSQL .= 'LIMIT 1';
$selectParam['Session_sNu'] = $this->_sessionNumber; # 自动编号
$selectParam['Session_sSn'] = $this->_sessionSn;
$selectParam['Session_sId'] = $this->_sessionId;


$queryInfo = $this->queryOne($selectSQL,$selectParam);
$this->_sessionIsQuery = (bool)$queryInfo;
$returnInfo = isset($queryInfo['Session_sData'])?$queryInfo['Session_sData']:null;
return $returnInfo;

}
/**
* 插入新的Session
*/
public function insertSession($value)
{
# 插入语句
$insertSQL = 'insert into `t_session_handler` ';
$insertSQL .= '(`Session_Id`,`Session_sSn`,`Session_sNu`,`Session_sId`,`Session_sData`,`Session_ExpireTime`)';
$insertSQL .= ' values ';
$insertSQL .= '(null,:Session_sSn,:Session_sNu,:Session_sId,:Session_sData,:Session_ExpireTime)';
# 插入参数
// $insertParam['Session_Id'] = null; # 自动编号
$insertParam['Session_sSn'] = $this->_sessionSn;
$insertParam['Session_sNu'] = $this->_sessionNumber; # 自动编号
$insertParam['Session_sId'] = $this->_sessionId;
$insertParam['Session_sData'] = $value;
$insertParam['Session_ExpireTime'] = $this->_sessionExpire; # 获取时间;
return $this->execute($insertSQL,$insertParam);
}
/**
* 更新session信息
*
* @return unknown
*/
public function updateSession($value)
{
# 修改语句
$updateSQL = 'update `t_session_handler` set ';
$updateSQL .= '`Session_sData` = :Session_sData ';
$updateSQL .= 'WHERE 1=1 '; #留空格
$updateSQL .= 'AND `Session_sSn`=:Session_sSn ';
$updateSQL .= 'AND `Session_sNu`=:Session_sNu ';
$updateSQL .= 'AND `Session_sId`=:Session_sId ';
$updateSQL .= 'LIMIT 1';

$updateParam['Session_sNu'] = $this->_sessionNumber; # 自动编号
$updateParam['Session_sSn'] = $this->_sessionSn;
$updateParam['Session_sId'] = $this->_sessionId;
$updateParam['Session_sData'] = $value;

return $this->execute($updateSQL,$updateParam);

}
/**
* 删除Session
*
* @return unknown
*/
public function delSession()
{
# 修改语句
$delSQL = 'delete from `t_session_handler` ';
$delSQL .= 'WHERE 1=1 '; #留空格
$delSQL .= 'AND `Session_sSn`=:Session_sSn ';
$delSQL .= 'AND `Session_sNu`=:Session_sNu ';
$delSQL .= 'AND `Session_sId`=:Session_sId ';
$delSQL .= 'LIMIT 1';
$delParam['Session_sNu'] = $this->_sessionNumber; # 自动编号
$delParam['Session_sSn'] = $this->_sessionSn;
$delParam['Session_sId'] = $this->_sessionId;
return $this->execute($delSQL,$delParam);

}
/**
* 删除过期 Session
*
* @return unknown
*/
public function expireSession($expireTime)
{
settype($expireTime,'integer');
# 修改语句
$delSQL = 'delete from `t_session_handler` ';
$delSQL .= ' WHERE 1=1 '; #留空格
$delSQL .= 'AND `Session_ExpireTime` < :Session_ExpireTime ';
$delSQL .= 'LIMIT 100 ;';
$delParam['Session_ExpireTime'] = time();
$returnInfo = $this->execute($delSQL,$delParam);
return $returnInfo['Session_sData'];

}
/**
* session 启动器
*
*/
public function on_session_start()
{
$this->_sessionExpire = time()+ini_get('session.gc_maxlifetime');
$this->_sessionId = session_id();
$this->getSessionPosition();
return true;
}
/**
* session 终止
*/
public function on_session_end()
{
return $this->expireSession($this->_sessionExpire);
}
/**
* 读取方法
*
* @param unknown_type $key
*/
public function on_session_read($key)
{
return $this->selectSession();
}
/**
* 写入方法
*
* @param unknown_type $key
* @param unknown_type $value
*/
public function on_session_write($key,$value)
{
$this->selectSession();
if ($this->_sessionIsQuery)
{
$this->updateSession($value);
}else{
$this->insertSession($value);
}
return true;
}
/**
* 销毁方法
*
* @param unknown_type $key
*/
public function on_session_destroy($key)
{
$this->getSessionPosition();
return $this->delSession();
}
/**
* 过期方法
*
* @param integer $maxLifeTime
*/
public function on_session_gc($maxLifeTime)
{
return $this->expireSession($maxLifeTime);
}
}
$sessionHandler = new SessionHandler();
session_set_save_handler(
array(&$sessionHandler,'on_session_start'),
array(&$sessionHandler,'on_session_end'),
array(&$sessionHandler,'on_session_read'),
array(&$sessionHandler,'on_session_write'),
array(&$sessionHandler,'on_session_destroy'),
array(&$sessionHandler,'on_session_gc')
);
unregister_tick_function('session_write_close');
?>
数据库结构
CREATE TABLE `t_session_handler` (
`Session_Id` int(11) NOT NULL auto_increment,
`Session_sSn` int(11) default NULL,
`Session_sNu` int(11) default NULL,
`Session_sId` varchar(50) default NULL,
`Session_sData` longtext,
`Session_ExpireTime` int(11) default NULL,
PRIMARY KEY (`Session_Id`),
KEY `Session_Id` (`Session_Id`),
KEY `Session_sSn` (`Session_sSn`),
KEY `Session_sNu` (`Session_sNu`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

索引还没建好.可以自己试着做.

多有不足,请斧正.

--------------
SysTem128
2007-12-15

MoXie

unread,
Dec 18, 2007, 3:02:34 AM12/18/07
to cnPhpDevelop
public function on_session_start()
{
$this->_sessionSite = 1;
$expireTime = ini_get('session.gc_maxlifetime');
$this->_sessionExpire = time()+$expireTime*60;
Reply all
Reply to author
Forward
0 new messages