-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessions.php
80 lines (67 loc) · 1.32 KB
/
sessions.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
class redis_sessions
{
private $redis = NULL;
private $session_name = NULL;
public $db = 0;
public $lifetime = NULL;
public $host = "127.0.0.1";
public $port = 6379;
public function __construct()
{
}
public function open($save_path, $session_name)
{
$this->session_name = $session_name;
if ($this->redis === NULL)
{
$this->redis = new php_redis($this->host, $this->port);
if ($this->db != 0)
{
$this->redis->select($this->db);
}
}
}
public function close()
{
$this->redis = NULL;
}
public function read($id)
{
$key = $this->session_name.":".$id;
$sess_data = $this->redis->get($key);
if ($sess_data === NULL)
{
return "";
}
return $sess_data;
}
public function write($id, $sess_data)
{
$key = $this->session_name.":".$id;
$lifetime = $this->lifetime;
if ($lifetime === NULL) {
$lifetime = ini_get("session.gc_maxlifetime");
}
$this->redis->set_expire($key, $lifetime, $sess_data);
}
public function destroy($id)
{
$key = $this->session_name.":".$id;
$this->redis->delete($key);
}
public function gc($maxlifetime)
{
}
public function install()
{
session_set_save_handler(
array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "destroy"),
array($this, "gc")
);
}
}