1、安裝phpredis擴展
2、重寫session開放接口
<?php
/**
* Created by PhpStorm.
* User: kungyu
* Date: 2015/11/24
* Time: 17:46
*/
class sessionManage
{
private $redis;
private $expireTime = 3600;
public function __construct()
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);
session_start();
}
public function open($path, $name)
{
return true;
}
public function close()
{
return true;
}
public function read($id)
{
$res = '';
if ($val = $this->redis->get($id))
$res = $val;
return $res;
}
public function write($id, $data)
{
$res = false;
if ($this->redis->set($id, $data)) {
$this->redis->expire($id, $this->expireTime);
$res = true;
}
return $res;
}
public function destroy($id)
{
$res = false;
if ($this->redis->delete($id))
$res = true;
return $res;
}
public function gc($maxLifeTime)
{
return true;
}
public function __destruct(){
session_write_close();
}
}
3、驗證使用
userlogin.php
include_once("./sessionManage.class.php");
$session_class = new sessionManage();
$_SESSION['username'] = 'kung';
echo $_SESSION['username'].'1111';
userlogout.php
include_once('./sessionManage.class.php');
$sess = new sessionManage();
echo $_SESSION['username'];