-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
NativeSessionStorage.php
155 lines (134 loc) · 4.17 KB
/
NativeSessionStorage.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
<?php
namespace Symfony\Component\HttpFoundation\SessionStorage;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* NativeSessionStorage.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class NativeSessionStorage implements SessionStorageInterface
{
static protected $sessionIdRegenerated = false;
static protected $sessionStarted = false;
protected $options;
/**
* Available options:
*
* * name: The cookie name (_SESSION by default)
* * id: The session id (null by default)
* * lifetime: Cookie lifetime
* * path: Cookie path
* * domain: Cookie domain
* * secure: Cookie secure
* * httponly: Cookie http only
*
* The default values for most options are those returned by the session_get_cookie_params() function
*
* @param array $options An associative array of options
*/
public function __construct(array $options = array())
{
$cookieDefaults = session_get_cookie_params();
$this->options = array_merge(array(
'name' => '_SESSION',
'lifetime' => $cookieDefaults['lifetime'],
'path' => $cookieDefaults['path'],
'domain' => $cookieDefaults['domain'],
'secure' => $cookieDefaults['secure'],
'httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
'cache_limiter' => 'none',
), $options);
session_name($this->options['name']);
}
/**
* Starts the session.
*/
public function start()
{
if (self::$sessionStarted) {
return;
}
session_set_cookie_params(
$this->options['lifetime'],
$this->options['path'],
$this->options['domain'],
$this->options['secure'],
$this->options['httponly']
);
if (null !== $this->options['cache_limiter']) {
session_cache_limiter($this->options['cache_limiter']);
}
if (!ini_get('session.use_cookies') && $this->options['id'] && $this->options['id'] != session_id()) {
session_id($this->options['id']);
}
session_start();
self::$sessionStarted = true;
}
/**
* Reads data from this storage.
*
* The preferred format for a key is directory style so naming conflicts can be avoided.
*
* @param string $key A unique key identifying your data
*
* @return mixed Data associated with the key
*/
public function read($key, $default = null)
{
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}
/**
* Removes data from this storage.
*
* The preferred format for a key is directory style so naming conflicts can be avoided.
*
* @param string $key A unique key identifying your data
*
* @return mixed Data associated with the key
*/
public function remove($key)
{
$retval = null;
if (isset($_SESSION[$key])) {
$retval = $_SESSION[$key];
unset($_SESSION[$key]);
}
return $retval;
}
/**
* Writes data to this storage.
*
* The preferred format for a key is directory style so naming conflicts can be avoided.
*
* @param string $key A unique key identifying your data
* @param mixed $data Data associated with your key
*
*/
public function write($key, $data)
{
$_SESSION[$key] = $data;
}
/**
* Regenerates id that represents this storage.
*
* @param boolean $destroy Destroy session when regenerating?
*
* @return boolean True if session regenerated, false if error
*
*/
public function regenerate($destroy = false)
{
if (self::$sessionIdRegenerated) {
return;
}
session_regenerate_id($destroy);
self::$sessionIdRegenerated = true;
}
}