1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
| <?php
class SessionManager{
private $redis;
private $sessionSavePath;
private $sessionName;
private $sessionExpireTime=30;//redis,session的过期时间为30s
public function __construct(){
$this->redis = new Redis();//创建phpredis实例
$this->redis->connect('127.0.0.1',6379);//连接redis
$this->redis->auth("107lab");//授权
$retval = 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){
$value = $this->redis->get($id);//获取redis中的指定记录
if($value){
return $value;
}else{
return '';
}
}
public function write($id,$data){
if($this->redis->set($id,$data)){//以session ID为键,存储
$this->redis->expire($id,$this->sessionExpireTime);//设置redis中数据的过期时间,即session的过期时间
return true;
}
return false;
}
public function destroy($id){
if($this->redis->delete($id)){//删除redis中的指定记录
return true;
}
return false;
}
public function gc($maxlifetime){
return true;
}
public function __destruct(){
session_write_close();
}
}
|