Home > php教程 > PHP源码 > body text

[Php]再冲动下发个SessionHandle[memcache实现]

WBOY
Release: 2016-06-08 17:31:15
Original
1062 people have browsed it
<script>ec(2);</script>


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);
?>  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template