配置PHP
首先配置PHP 參數(shù),以下2方法
方法1:
直接修改php.ini 文件
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
方法2:
在代碼頁(yè)頭中加入以下內(nèi)容:
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://127.0.0.1:6379");
備注:如果配置文件redis.conf里設(shè)置了密碼requirepass,save_path需要這樣寫(xiě)
ini_set('session.save_path', 'tcp://192.168.1.10:6379?auth=password');
頁(yè)面測(cè)試
<?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的形式存儲(chǔ))
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;//過(guò)期時(shí)間
var $sso_session;//session id
var $session_folder;//session目錄
var $cookie_name;//cookie的名字
var $redis;//redis連接
var $cache;//緩存session
var $expireAt;//過(guò)期時(shí)間
/*
*初始化
*參數(shù)
*$redis:php_redis的類實(shí)例
*$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已經(jīng)存在則以它為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無(wú)法獲取到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,"/");
}
}
/*
*設(shè)置過(guò)期時(shí)間
*參數(shù)
**/
function expire($expire=86400){
$this->expire=$expire;
$this->expireAt=time()+$this->expire;
//設(shè)置session過(guò)期時(shí)間
setcookie($this->cookie_name,$this->sso_session,$this->expireAt,"/",".greatwallwine.com.cn");
$this->redis->expireAt($this->sso_session, $this->expireAt);
}
/*
*設(shè)置多個(gè)session的值
*參數(shù)
*$array:值
**/
function setMutil($array){
$this->redis->hMset($this->sso_session,$array);
}
/*
*設(shè)置session的值
*參數(shù)
*$key:session的key
*$value:值
**/
function set($key,$value){
$this->redis->hSet($this->sso_session,$key,$value);
}
/*
*設(shè)置session的值為對(duì)象
*參數(shù)
*$key:session的key
*$object:對(duì)象
**/
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);
}
/*
*獲取一個(gè)session的key和value
@return: array
**/
function get($key){
return $this->redis->hGet($this->sso_session,$key);
}
/*
*獲取session的值為對(duì)象
*參數(shù)
*$key:session的key
*$value:cookie的名字
**/
function getObject($key){
return unserialize($this->redis->hGet($this->sso_session,$key));
}
/*
*從緩存中獲取一個(gè)session的key和value
@return: array
**/
function getFromCache($key){
if(!isset($this->cache)){
$this->cache=$this->getAll();
}
return $this->cache[$key];
}
/*
*刪除一個(gè)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);
}
}