1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Session; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for wrapping sessions. |
7
|
|
|
*/ |
8
|
|
|
class Session implements SessionInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Set a session name. |
12
|
|
|
* |
13
|
|
|
* @param string $name to set as session name. |
14
|
|
|
* |
15
|
|
|
* @return self |
16
|
|
|
*/ |
17
|
2 |
|
public function name($name) |
18
|
|
|
{ |
19
|
2 |
|
session_name($name); |
20
|
2 |
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Start the session (ignore when on cli). |
27
|
|
|
* |
28
|
|
|
* @return self |
29
|
|
|
*/ |
30
|
|
|
public function start() |
31
|
|
|
{ |
32
|
|
|
if (php_sapi_name() !== 'cli') { |
33
|
|
|
session_start(); |
34
|
|
|
} |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if a value is set in the session. |
42
|
|
|
* |
43
|
|
|
* @param string $key in session variable. |
44
|
|
|
* |
45
|
|
|
* @return boolean true if $key is set, else false. |
46
|
|
|
* |
47
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
48
|
|
|
*/ |
49
|
2 |
|
public function has($key) |
50
|
|
|
{ |
51
|
2 |
|
return isset($_SESSION) && isset($_SESSION[$key]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a value from the session. |
58
|
|
|
* |
59
|
|
|
* @param string $key in session variable. |
60
|
|
|
* @param mixed $default default value to return when key is not set |
61
|
|
|
* in the session. |
62
|
|
|
* |
63
|
|
|
* @return mixed value from session and null if not set. |
64
|
|
|
* |
65
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
66
|
|
|
*/ |
67
|
2 |
|
public function get($key, $default = null) |
68
|
|
|
{ |
69
|
2 |
|
return $this->has($key) |
70
|
2 |
|
? $_SESSION[$key] |
71
|
2 |
|
: $default; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Read a value from the session and unset it, this is a form of read |
78
|
|
|
* once flash memory using the session. |
79
|
|
|
* |
80
|
|
|
* @param string $key in session variable. |
81
|
|
|
* @param mixed $default default value to return when key is not set |
82
|
|
|
* in the session. |
83
|
|
|
* |
84
|
|
|
* @return mixed value from session and null if not set. |
85
|
|
|
*/ |
86
|
1 |
|
public function getOnce($key, $default = null) |
87
|
|
|
{ |
88
|
1 |
|
$read = $this->get($key, $default); |
89
|
1 |
|
$this->delete($key); |
90
|
1 |
|
return $read; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set values in session. |
97
|
|
|
* |
98
|
|
|
* @param string $key in session variable. |
99
|
|
|
* @param mixed $value to set in session. |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
* |
103
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
104
|
|
|
*/ |
105
|
2 |
|
public function set($key, $value) |
106
|
|
|
{ |
107
|
2 |
|
$_SESSION[$key] = $value; |
108
|
2 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Unset session value of this key. |
115
|
|
|
* |
116
|
|
|
* @param string $key in session variable. |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
* |
120
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
121
|
|
|
*/ |
122
|
1 |
|
public function delete($key) |
123
|
|
|
{ |
124
|
1 |
|
if (isset($_SESSION[$key])) { |
125
|
1 |
|
unset($_SESSION[$key]); |
126
|
1 |
|
} |
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Destroy the session. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
* |
137
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
138
|
|
|
*/ |
139
|
|
|
public function destroy() |
140
|
|
|
{ |
141
|
|
|
// Unset all of the session variables. |
142
|
|
|
$_SESSION = array(); |
143
|
|
|
|
144
|
|
|
// Delete the session cookie. |
145
|
|
|
if (ini_get("session.use_cookies")) { |
146
|
|
|
$params = session_get_cookie_params(); |
147
|
|
|
setcookie( |
148
|
|
|
session_name(), |
149
|
|
|
'', |
150
|
|
|
time() - 42000, |
151
|
|
|
$params["path"], |
152
|
|
|
$params["domain"], |
153
|
|
|
$params["secure"], |
154
|
|
|
$params["httponly"] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Finally, destroy the session. |
159
|
|
|
session_destroy(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* To enable var_dump() of the session object. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
* |
169
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
170
|
|
|
*/ |
171
|
|
|
public function __debugInfo() |
172
|
|
|
{ |
173
|
|
|
return $_SESSION; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|