[Php]再冲动下发个SessionHandle[memcache实现]
<?php
/**
* session处理器
* memcache 实现
* @author MoXie SysTem128@GMail.Com
*/
class SessionHandler extends Memcache
{
public $_sessionId; # session 编号
public $_sessionExpire; # session 过期时间
public $_sessionIsQuery;
public function __construct()
{
$this->_sessionExpire = 600;
$this->connect(''localhost'',11211);
}
/**
* 获取所有定位字段
*
*/
/**
* 读取 Session
*
* @return unknown
*/
public function getSession($snKey)
{
$returnInfo = $this->get($snKey);
$this->_sessionIsQuery = (bool)$returnInfo;
return $returnInfo;
}
/**
* 插入新的Session
*/
public function insertSession($snKey,$value)
{
return $this->set($snKey,$value,0,$this->_sessionExpire);
}
/**
* 更新session信息
*
* @return unknown
*/
public function updateSession($snKey,$value)
{
# 修改语句
return $this->set($snKey,$value,0,$this->_sessionExpire);
}
/**
* 删除Session
*
* @return unknown
*/
public function delSession($snKey)
{
return $this->delete($snKey,0);
}
/**
* 删除过期 Session
*
* @return unknown
*/
public function expireSession()
{
return true;
}
/**
* session 启动器
*
*/
public function on_session_start()
{
$this->_sessionSite = 1;
$this->_sessionId = session_id();
return true;
}
/**
* session 终止
*/
public function on_session_end()
{
return $this->expireSession();
}
/**
* 读取方法
*
* @param unknown_type $key
*/
public function on_session_read($key)
{
return $this->getSession($key);
}
/**
* 写入方法
*
* @param unknown_type $key
* @param unknown_type $value
*/
public function on_session_write($key,$value)
{
$this->getSession($key);
if ($this->_sessionIsQuery)
{
$this->updateSession($key,$value);
}else{
$this->insertSession($key,$value);
}
return true;
}
/**
* 销毁方法
*
* @param unknown_type $key
*/
public function on_session_destroy($key)
{
return $this->delSession();
}
/**
* 过期方法
*
* @param integer $maxLifeTime
*/
public function on_session_gc($maxLifeTime)
{
return $this->expireSession();
}
}
$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'');
session_start();
//$_SESSION[''MoXie''] = ''Wonderfull!'';
//$_SESSION[''SysTem128''] = ''Wonderfull!'';
//session_unregister(''MoXie'');
print_r($_SESSION);
?>
补充:Php教程,Php常用代码