[转载]用redis实现跨服务器session

地址:http://blog.chinaunix.net/uid-11121450-id-3284875.html

这个月我们新开发了一个项目,由于使用到了4台机器做web,使用dns做负载均衡,


上面图上用户通过DNS的调度(一个域名对应多个ip)分别访问到VM2-VM5上,四台机器都访问VM1上的redis,两个redis值主从结构.
因此需要使用跨服务器的session保存用户登录状态,于是我写了一个跨站的session共享的类

点击(此处)折叠或打开

  1. <?php
  2.     /*
  3.      *用redis实现跨服务器session
  4.      *注意需要安装phpredis模块
  5.      *
  6.      *作者:yifangyou
  7.      *日期:2012-07-23 22:55:00
  8.     **/
  9. class RedisSession{
  10.     var $expire=86400;//过期时间
  11.     var $sso_session;//session id
  12.     var $session_folder;//session目录
  13.     var $cookie_name;//cookie的名字
  14.     var $redis;//redis连接
  15.     var $cache;//缓存session
  16.     var $expireAt;//过期时间
  17.     /*
  18.      *初始化
  19.      *参数
  20.      *$redis:php_redis的类实例
  21.      *$cookie_name:cookie的名字
  22.      *$session_id_prefix:sesion id的前缀
  23.     **/
  24.     function RedisSession($redis,$expire=86400,$cookie_name="sso_session",$session_id_prefix=""){
  25.         $this->redis=$redis;
  26.         $this->cookie_name=$cookie_name;
  27.         $this->session_folder="sso_session:";
  28.     //若是cookie已经存在则以它为session的id
  29.         if(isset($_COOKIE[$this->cookie_name])){
  30.              $this->sso_session=$_COOKIE[$this->cookie_name];
  31.         }else{
  32.             $this->expire=$expire;
  33.             $this->expireAt=time()+$this->expire;
  34.          //在IE6下的iframe无法获取到cookie,于是我使用了get方式传递了cookie的名字
  35.             if(isset($_GET[$this->cookie_name])){
  36.                     $this->sso_session=$_GET[$this->cookie_name];
  37.             }else{
  38.                     $this->sso_session=$this->session_folder.$session_prefix.md5(uniqid(rand(), true));    
  39.             }
  40.             setcookie($this->cookie_name,$this->sso_session,$this->expireAt,"/");
  41.         }
  42.     }
  43.     
  44.     /*
  45.      *设置过期时间
  46.      *参数
  47.     **/
  48.     function expire($expire=86400){
  49.             $this->expire=$expire;
  50.             $this->expireAt=time()+$this->expire;
  51.             //设置session过期时间
  52.             setcookie($this->cookie_name,$this->sso_session,$this->expireAt,"/",".greatwallwine.com.cn");
  53.             $this->redis->expireAt($this->sso_session, $this->expireAt);
  54.     }
  55.     
  56.     /*
  57.      *设置多个session的值
  58.      *参数
  59.      *$array:值
  60.     **/
  61.     function setMutil($array){
  62.         $this->redis->hMset($this->sso_session,$array);
  63.     }
  64.     /*
  65.      *设置session的值
  66.      *参数
  67.      *$key:session的key
  68.      *$value:值
  69.     **/
  70.     function set($key,$value){
  71.         $this->redis->hSet($this->sso_session,$key,$value);
  72.     }
  73.     /*
  74.      *设置session的值为对象
  75.      *参数
  76.      *$key:session的key
  77.      *$object:对象
  78.     **/
  79.     function setObject($key,$object){
  80.         $this->redis->hSet($this->sso_session,$key,serialize($object));
  81.     }
  82.     
  83.     /*
  84.      *获取全部session的key和value
  85.      @return: array
  86.     **/
  87.     function getAll(){
  88.         return $this->redis->hGetAll($this->sso_session);
  89.     }
  90.     
  91.     
  92.     
  93.     /*
  94.      *获取一个session的key和value
  95.      @return: array
  96.     **/
  97.     function get($key){
  98.         return $this->redis->hGet($this->sso_session,$key);
  99.     }
  100.     
  101.   /*
  102.      *获取session的值为对象
  103.      *参数
  104.      *$key:session的key
  105.      *$value:cookie的名字
  106.     **/
  107.     function getObject($key){
  108.         return unserialize($this->redis->hGet($this->sso_session,$key));
  109.     }
  110.     /*
  111.      *从缓存中获取一个session的key和value
  112.      @return: array
  113.     **/
  114.     function getFromCache($key){
  115.         if(!isset($this->cache)){
  116.             $this->cache=$this->getAll();
  117.         }
  118.         return $this->cache[$key];
  119.     }
  120.     
  121.     /*
  122.      *删除一个session的key和value
  123.      @return: array
  124.     **/
  125.     function del($key){
  126.         return $this->redis->hDel($this->sso_session,$key);
  127.     }
  128.     /*
  129.      *删除所有session的key和value
  130.      @return: array
  131.     **/
  132.     function delAll(){
  133.         return $this->redis->delete($this->sso_session);
  134.     }
  135. }
  136. ?>
使用方法:

点击(此处)折叠或打开

  1. <?php
  2. error_reporting(0);
  3. $redisHost="192.168.1.2";
  4. $redisPort="6379";
  5. $redis = new Redis();
  6. $redis->connect($redisHost,$redisPort);
  7. include_once("inc/RedisSession.php");
  8. $redisSession=new RedisSession($redis);
  9. /*
  10. $redisSession->set("name","sdf4");
  11. $redisSession->set("age",1234);
  12. $redisSession->set("***","man14");
  13. $redisSession->set("name","abc4");
  14. $redisSession->setMutil(array("province"=>"guangdong","city"=>"guangzhou"));
  15. */
  16. $redisSession->setObject("obj",array("test1"=>array("test2")));
  17. $obj=$redisSession->getObject("obj");
  18. print_r($obj);
  19. die();
  20. print_r($redisSession->getAll());
  21. //$redisSession->del("name");
  22. print_r($redisSession->get("name"));
  23. //print_r($redisSession->get("province"));
  24. //$redisSession->delAll();
  25. //print_r($redisSession->getAll());
  26. print_r($redisSession->getFromCache("name"));
  27. /*
  28.     $redisSession->del("name");
  29.     $redisSession->delAll();
  30. */
比较常用的估计是set,get,setObject,getOject
我用sso_session:我主要是方便用phpRedisAdmin管理
 
posted @ 2015-12-18 11:06  realhero  阅读(863)  评论(0编辑  收藏  举报