-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.php
355 lines (319 loc) · 8.32 KB
/
Session.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace Rad\Network;
use ArrayAccess;
use Countable;
use Iterator;
use JsonSerializable;
use Serializable;
use SessionHandlerInterface;
use Rad\Network\Session\Handler\NativeSessionHandler;
/**
* RadPHP Session
*
* @package Rad\Network
*/
class Session implements ArrayAccess, Iterator, Serializable, JsonSerializable, Countable
{
protected $started = false;
protected $session = [];
const SESSION_NAME = 'RADPHPSESSID';
/**
* Rad\Network\Session constructor
*
* @param SessionHandlerInterface $handler Session handler
* @param string $name Session name
*/
public function __construct(SessionHandlerInterface $handler = null, $name = self::SESSION_NAME)
{
if (null === $handler) {
$handler = new NativeSessionHandler();
}
session_set_save_handler($handler);
$this->setName($name);
}
/**
* Starts the session (if headers are already sent the session will not be started)
*
* @return boolean
*/
public function start()
{
if (headers_sent() === false) {
session_start();
$this->started = true;
$this->session = &$_SESSION;
return true;
}
return false;
}
/**
* Gets a session variable
*
* @param string $key
* @param mixed $defaultValue
*
* @return mixed
*/
public function get($key, $defaultValue = null)
{
if (isset($this->session[$key]) && !empty($this->session[$key])) {
return $this->session[$key];
}
return $defaultValue;
}
/**
* Sets a session variable
*
* @param string $key
* @param string $value
*/
public function set($key, $value)
{
$this->session[$key] = $value;
}
/**
* Check whether a session variable is set
*
* @param string $key
*
* @return bool
*/
public function has($key)
{
return isset($this->session[$key]);
}
/**
* Removes a session variable
*
* @param string $key
*/
public function remove($key)
{
unset($this->session[$key]);
}
/**
* Set the current session id
*
* @param string $id
*/
public function setId($id)
{
session_id($id);
}
/**
* Returns active session id
*
* @return string
*/
public function getId()
{
return session_id();
}
/**
* Update the current session id with a newly generated one
*
* @param bool $deleteOldSession Whether to delete the old associated session file or not.
*
* @link http://php.net/manual/en/function.session-regenerate-id.php
* @return bool true on success or false on failure.
*/
public function regenerateId($deleteOldSession = false)
{
return session_regenerate_id($deleteOldSession);
}
/**
* Set the current session name
*
* @param string $name The session name references the name of the session, which is
* used in cookies and URLs (e.g. PHPSESSID). It
* should contain only alphanumeric characters; it should be short and
* descriptive (i.e. for users with enabled cookie warnings).
* If "name" is specified, the name of the current
* session is changed to its value.
* The session name can't consist of digits only, at least one letter
* must be present. Otherwise a new session id is generated every time.
*
* @link http://php.net/manual/en/function.session-name.php
* @return string the name of the current session.
*/
public function setName($name)
{
session_name($name);
}
/**
* Get the current session name
*
* @link http://php.net/manual/en/function.session-name.php
* @return string the name of the current session.
*/
public function getName()
{
return session_name();
}
/**
* Check whether the session has been started
*
* @return bool
*/
public function isStarted()
{
return $this->started;
}
/**
* Destroys the active session
*
* @return bool
*/
public function destroy()
{
if ($this->started === true) {
$this->started = false;
return session_destroy();
}
return false;
}
/**
* Whether a offset exists
*
* @param mixed $offset An offset to check for.
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @return boolean true on success or false on failure. The return value
* will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* Offset to retrieve
*
* @param mixed $offset The offset to retrieve.
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Offset to set
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @return void
*/
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Offset to unset
*
* @param mixed $offset The offset to unset.
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @return void
*/
public function offsetUnset($offset)
{
$this->remove($offset);
}
/**
* Return the current element
*
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
return current($_SESSION);
}
/**
* Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
next($_SESSION);
}
/**
* Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
return key($_SESSION);
}
/**
* Checks if current position is valid
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
return $this->current() !== false;
}
/**
* Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
reset($this->session);
}
/**
* String representation of object
*
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
*/
public function serialize()
{
return serialize($this->session);
}
/**
* Constructs the object
*
* @param string $serialized The string representation of the object.
*
* @link http://php.net/manual/en/serializable.unserialize.php
* @return void
*/
public function unserialize($serialized)
{
$this->session = unserialize($serialized);
}
/**
* Count elements of an object
*
* @return int The custom count as an integer. The return value is cast to an integer.
* @link http://php.net/manual/en/countable.count.php
*/
public function count()
{
return count($this->session);
}
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by json_encode,
* which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return $this->session;
}
}