PHP进阶

PHP加速,使用Redis共享SESSION

2019-04-20  本文已影响0人  IM魂影

配置PHP

首先配置PHP 参数,以下2方法

方法1:

直接修改php.ini 文件

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

方法2:

在代码页头中加入以下内容:

ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379");

备注:如果配置文件redis.conf里设置了密码requirepass,save_path需要这样写

ini_set('session.save_path', 'tcp://192.168.1.10:6379?auth=password');

页面测试

<?php
//ini_set("session.save_handler", "redis");
//ini_set("session.save_path", "tcp://127.0.0.1:6379");

session_start();

//存入session
$_SESSION['class'] = array('name' => 'Alicelock', 'num' => 21);

//连接redis
$redis = new redis();
$redis->connect('127.0.0.1', 6379);

//检查session_id
echo 'session_id:' . session_id() . '<br/>';

//redis存入的session(redis用session_id作为key,以string的形式存储)
echo 'redis_session:' . $redis->get('PHPREDIS_SESSION:' . session_id()) . '<br/>';

//php获取session值
echo 'php_session:' . json_encode($_SESSION['class']);

封装好的类

直接拿去用吧

<?php 
class RedisSession{  
    var $expire=86400;//过期时间  
    var $sso_session;//session id  
    var $session_folder;//session目录  
    var $cookie_name;//cookie的名字  
    var $redis;//redis连接  
    var $cache;//缓存session  
    var $expireAt;//过期时间  
    /*  
     *初始化  
     *参数  
     *$redis:php_redis的类实例  
     *$cookie_name:cookie的名字  
     *$session_id_prefix:sesion id的前缀  
    **/ 
    function RedisSession($redis,$expire=86400,$cookie_name="sso_session",$session_id_prefix=""){  
        $this->redis=$redis;  
        $this->cookie_name=$cookie_name;  
        $this->session_folder="sso_session:";  
    //若是cookie已经存在则以它为session的id  
        if(isset($_COOKIE[$this->cookie_name])){  
             $this->sso_session=$_COOKIE[$this->cookie_name];  
        }else{  
            $this->expire=$expire;  
            $this->expireAt=time()+$this->expire;  
         //在IE6下的iframe无法获取到cookie,于是我使用了get方式传递了cookie的名字  
            if(isset($_GET[$this->cookie_name])){  
                    $this->sso_session=$_GET[$this->cookie_name];  
            }else{  
                    $this->sso_session=$this->session_folder.$session_prefix.md5(uniqid(rand(), true));      
            }  
            setcookie($this->cookie_name,$this->sso_session,$this->expireAt,"/");  
        }  
    }  
      
    /*  
     *设置过期时间  
     *参数  
    **/ 
    function expire($expire=86400){  
            $this->expire=$expire;  
            $this->expireAt=time()+$this->expire;  
            //设置session过期时间  
            setcookie($this->cookie_name,$this->sso_session,$this->expireAt,"/",".greatwallwine.com.cn");  
            $this->redis->expireAt($this->sso_session, $this->expireAt);  
    }  
      
    /*  
     *设置多个session的值  
     *参数  
     *$array:值  
    **/ 
    function setMutil($array){  
        $this->redis->hMset($this->sso_session,$array);  
    }  
    /*  
     *设置session的值  
     *参数  
     *$key:session的key  
     *$value:值  
    **/ 
    function set($key,$value){  
        $this->redis->hSet($this->sso_session,$key,$value);  
    }  
    /*  
     *设置session的值为对象  
     *参数  
     *$key:session的key  
     *$object:对象  
    **/ 
    function setObject($key,$object){  
        $this->redis->hSet($this->sso_session,$key,serialize($object));  
    }  
      
    /*  
     *获取全部session的key和value  
     @return: array  
    **/ 
    function getAll(){  
        return $this->redis->hGetAll($this->sso_session);  
    }  
      
      
      
    /*  
     *获取一个session的key和value  
     @return: array  
    **/ 
    function get($key){  
        return $this->redis->hGet($this->sso_session,$key);  
    }  
      
  /*  
     *获取session的值为对象  
     *参数  
     *$key:session的key  
     *$value:cookie的名字  
    **/ 
    function getObject($key){  
        return unserialize($this->redis->hGet($this->sso_session,$key));  
    }  
    /*  
     *从缓存中获取一个session的key和value  
     @return: array  
    **/ 
    function getFromCache($key){  
        if(!isset($this->cache)){  
            $this->cache=$this->getAll();  
        }  
        return $this->cache[$key];  
    }  
      
    /*  
     *删除一个session的key和value  
     @return: array  
    **/ 
    function del($key){  
        return $this->redis->hDel($this->sso_session,$key);  
    }  
    /*  
     *删除所有session的key和value  
     @return: array  
    **/ 
    function delAll(){  
        return $this->redis->delete($this->sso_session);  
    }  
} 

上一篇下一篇

猜你喜欢

热点阅读