实现基于Memcache存储的Session类

  1. <?php
  2. //===========================================
  3. // 程序:Memcache-Based Session Class
  4. // 功能:基于Memcache存储的 Session 功能类
  5. // 作者: heiyeluren
  6. // 博客: http://blog.csdn.net/heiyeshuwu
  7. // 时间: 2006-12-23
  8. //===========================================
  9. /**
  10.  * 类名: FileSession Class
  11.  * 功能: 自主实现基于Memcache存储的 Session 功能
  12.  * 描述: 这个类就是实现Session的功能, 基本上是通过设置客户端的Cookie来保存SessionID,
  13.  *         然后把用户的数据保存在服务器端,最后通过Cookie中的Session Id来确定一个数据是否是用户的, 
  14.  *         然后进行相应的数据操作, 目前的缺点是没有垃圾收集功能
  15.  *
  16.  *        本方式适合Memcache内存方式存储Session数据的方式,同时如果构建分布式的Memcache服务器,
  17.  *        能够保存相当多缓存数据,并且适合用户量比较多并发比较大的情况
  18.  * 注意: 本类必须要求PHP安装了Memcache扩展, 获取Memcache扩展请访问: http://pecl.php.net
  19.  */
  20. class MemcacheSession
  21. {
  22.     var $sessId                = '';
  23.     var $sessKeyPrefix         = 'sess_';
  24.     var $sessExpireTime        = 86400;
  25.     var $cookieName         = '__SessHandler';
  26.     var $cookieExpireTime     = '';    
  27.     var $memConfig             = array('host'=>'192.168.0.200''port'=>11211);
  28.     var $memObject            = null;    
  29.     
  30.     
  31.     /**
  32.      * 构造函数
  33.      *
  34.      * @param bool $isInit - 是否实例化对象的时候启动Session
  35.      */
  36.     function MemcacheSession($isInit = false){
  37.         if ($isInit){
  38.             $this->start();
  39.         }
  40.     }
  41.     //-------------------------
  42.     //   外部方法
  43.     //-------------------------
  44.     
  45.     /**
  46.      * 启动Session操作
  47.      *
  48.      * @param int $expireTime - Session失效时间,缺省是0,当浏览器关闭的时候失效, 该值单位是秒
  49.      */
  50.     function start($expireTime = 0){
  51.         $sessId = $_COOKIE[$this->cookieName];
  52.         if (!$sessId){
  53.             $this->sessId = $this->_getId();
  54.             $this->cookieExpireTime = ($expireTime > 0) ? time() + $expireTime : 0;
  55.             setcookie($this->cookieName, $this->sessId, $this->cookieExpireTime, "/"'');
  56.             $this->_initMemcacheObj();
  57.             $_SESSION = array();
  58.             $this->_saveSession();
  59.         } else {
  60.             $this->sessId = $sessId;
  61.             $_SESSION = $this->_getSession($sessId);
  62.         }        
  63.     }
  64.     
  65.     /**
  66.      * 判断某个Session变量是否注册
  67.      *
  68.      * @param string $varName - 
  69.      * @return bool 存在返回true, 不存在返回false
  70.      */
  71.     function is_registered($varName){
  72.         if (!isset($_SESSION[$varName])){
  73.             return false;
  74.         }
  75.         return true;
  76.     }    
  77.         
  78.     /**
  79.      * 注册一个Session变量
  80.      *
  81.      * @param string $varName - 需要注册成Session的变量名
  82.      * @param mixed $varValue - 注册成Session变量的值
  83.      * @return bool - 该变量名已经存在返回false, 注册成功返回true
  84.      */
  85.     function register($varName$varValue){
  86.         if (isset($_SESSION[$varName])){
  87.             return false;
  88.         }
  89.         $_SESSION[$varName] = $varValue;
  90.         $this->_saveSession();
  91.         return true;
  92.     }
  93.     
  94.     /**
  95.      * 销毁一个已注册的Session变量
  96.      *
  97.      * @param string $varName - 需要销毁的Session变量名
  98.      * @return bool 销毁成功返回true
  99.      */
  100.     function unregister($varName){
  101.         unset($_SESSION[$varName]);
  102.         $this->_saveSession();
  103.         return true;
  104.     }
  105.     
  106.     /**
  107.      * 销毁所有已经注册的Session变量
  108.      *
  109.      * @return 销毁成功返回true
  110.      */
  111.     function destroy(){
  112.         $_SESSION = array();
  113.         $this->_saveSession();
  114.         return true;    
  115.     }
  116.     
  117.     /**
  118.      * 获取一个已注册的Session变量值
  119.      *
  120.      * @param string $varName - Session变量的名称
  121.      * @return mixed - 不存在的变量返回false, 存在变量返回变量值
  122.      */
  123.     function get($varName){
  124.         if (!isset($_SESSION[$varName])){
  125.             return false;
  126.         }
  127.         return $_SESSION[$varName];
  128.     }    
  129.     
  130.     /**
  131.      * 获取所有Session变量
  132.      *
  133.      * @return array - 返回所有已注册的Session变量值
  134.      */
  135.     function getAll(){
  136.         return $_SESSION;
  137.     }
  138.     
  139.     /**
  140.      * 获取当前的Session ID
  141.      *
  142.      * @return string 获取的SessionID
  143.      */
  144.     function getSid(){
  145.         return $this->sessId;
  146.     }
  147.     /**
  148.      * 获取Memcache的配置信息
  149.      *
  150.      * @return array Memcache配置数组信息
  151.      */
  152.     function getMemConfig(){
  153.         return $this->memConfig;
  154.     }
  155.     
  156.     /**
  157.      * 设置Memcache的配置信息
  158.      *
  159.      * @param string $host - Memcache服务器的IP
  160.      * @param int $port - Memcache服务器的端口
  161.      */
  162.     function setMemConfig($host$port){
  163.         $this->memConfig = array('host'=>$host'port'=>$port);
  164.     }    
  165.     
  166.     
  167.     //-------------------------
  168.     //   内部接口
  169.     //-------------------------
  170.     
  171.     /**
  172.      * 生成一个Session ID
  173.      *
  174.      * @return string 返回一个32位的Session ID
  175.      */
  176.     function _getId(){
  177.         return md5(uniqid(microtime()));
  178.     }
  179.     
  180.     /**
  181.      * 获取一个保存在Memcache的Session Key
  182.      *
  183.      * @param string $sessId - 是否指定Session ID
  184.      * @return string 获取到的Session Key
  185.      */
  186.     function _getSessKey($sessId = ''){
  187.         $sessKey = ($sessId == '') ? $this->sessKeyPrefix.$this->sessId : $this->sessKeyPrefix.$sessId;
  188.         return $sessKey;
  189.     }    
  190.     /**
  191.      * 检查保存Session数据的路径是否存在
  192.      *
  193.      * @return bool 成功返回true
  194.      */
  195.     function _initMemcacheObj(){
  196.         if (!class_exists('Memcache') || !function_exists('memcache_connect')){
  197.             $this->_showMessage('Failed: Memcache extension not install, please from http://pecl.php.net download and install');
  198.         }        
  199.         if ($this->memObject && is_object($this->memObject)){
  200.             return true;
  201.         }
  202.         $mem = new Memcache;
  203.         if (!@$mem->connect($this->memConfig['host'], $this->memConfig['port'])){
  204.             $this->_showMessage('Failed: Connect memcache host '$this->memConfig['host'] .':'$this->memConfig['port'] .' failed');
  205.         }
  206.         $this->memObject = $mem;
  207.         return true;
  208.     }
  209.     
  210.     /**
  211.      * 获取Session文件中的数据
  212.      *
  213.      * @param string $sessId - 需要获取Session数据的SessionId
  214.      * @return unknown
  215.      */
  216.     function _getSession($sessId = ''){
  217.         $this->_initMemcacheObj();
  218.         $sessKey = $this->_getSessKey($sessId);
  219.         $sessData = $this->memObject->get($sessKey);
  220.         if (!is_array($sessData) || emptyempty($sessData)){
  221.             $this->_showMessage('Failed: Session ID '$sessKey .' session data not exists');
  222.         }
  223.         return $sessData;
  224.     }
  225.     
  226.     /**
  227.      * 把当前的Session数据保存到Memcache
  228.      *
  229.      * @param string $sessId - Session ID
  230.      * @return 成功返回true
  231.      */
  232.     function _saveSession($sessId = ''){
  233.         $this->_initMemcacheObj();
  234.         $sessKey = $this->_getSessKey($sessId);
  235.         if (emptyempty($_SESSION)){
  236.             $ret = @$this->memObject->set($sessKey$_SESSION, false, $this->sessExpireTime);
  237.         }else{
  238.             $ret = @$this->memObject->replace($sessKey$_SESSION, false, $this->sessExpireTime);
  239.         }
  240.         if (!$ret){
  241.             $this->_showMessage('Failed: Save sessiont data failed, please check memcache server');
  242.         }
  243.         return true;
  244.     }
  245.     
  246.     /**
  247.      * 显示提示信息
  248.      *
  249.      * @param string $strMessage - 需要显示的信息内容
  250.      * @param bool $isFailed - 是否是失败信息, 缺省是true
  251.      */
  252.     function _showMessage($strMessage$isFailed = true){
  253.         if ($isFailed){
  254.             exit($strMessage);
  255.         }
  256.         echo $strMessage;
  257.     }    
  258. }
  259. ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值