1 | <?php declare(strict_types=1); |
||
5 | final class SessionManager |
||
6 | { |
||
7 | const IP_REGEX = '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/'; |
||
8 | |||
9 | /** |
||
10 | * As this is a singleton, construction and clone are disabled |
||
11 | * use SessionManager::getInstance() if you need the instance |
||
12 | */ |
||
13 | private function __construct() |
||
16 | |||
17 | private function __clone() |
||
20 | |||
21 | /** |
||
22 | * @return SessionManager |
||
23 | */ |
||
24 | public static function getInstance(): SessionManager |
||
34 | |||
35 | /** |
||
36 | * Creates a secure session |
||
37 | * |
||
38 | * @param string $name |
||
39 | * @param int $lifetime |
||
40 | * @param string $path |
||
41 | * @param string $domain |
||
42 | * @param bool|null $secure |
||
43 | */ |
||
44 | public static function sessionStart(string $name, int $lifetime = 0, string $path = '/', string $domain = '', ?bool $secure = null): void |
||
66 | |||
67 | private function initialise(): void |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | private function shouldRandomlyRegenerate(): bool |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Checks session IP and user agent are still the same |
||
98 | * @return bool |
||
99 | */ |
||
100 | private function isHijackAttempt(): bool |
||
119 | |||
120 | /** |
||
121 | * If a site goes through the likes of Cloudflare, the last part of the IP might change |
||
122 | * So we replace it with an x. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | private function getIpAddress(): string |
||
132 | |||
133 | /** |
||
134 | * Creates a fresh session Id to make it harder to hack |
||
135 | * If the site is very slow in parts increase the expiry time |
||
136 | * 10 seconds is a good default which allows ajax calls to work |
||
137 | * without losing the session |
||
138 | */ |
||
139 | private function regenerateSession() |
||
165 | |||
166 | /** |
||
167 | * Checks whether the session has expired or not |
||
168 | * @return bool |
||
169 | */ |
||
170 | private function isValid(): bool |
||
182 | |||
183 | /** |
||
184 | * Resets the session |
||
185 | */ |
||
186 | public static function destroySession() |
||
196 | |||
197 | /** |
||
198 | * @param string $key |
||
199 | * @param mixed $val |
||
200 | */ |
||
201 | public function set(string $key, $val): void |
||
205 | |||
206 | /** |
||
207 | * @param $key |
||
208 | * @return null|mixed |
||
209 | */ |
||
210 | public function get(string $key) |
||
214 | |||
215 | /** |
||
216 | * @param string $key |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function has(string $key): bool |
||
223 | |||
224 | /** |
||
225 | * @param $key |
||
226 | * @param $val |
||
227 | */ |
||
228 | public function unset(string $key): void |
||
232 | |||
233 | /** |
||
234 | * @param $key |
||
235 | * @param $val |
||
236 | * @deprecated use unset |
||
237 | */ |
||
238 | public function destroy(string $key): void |
||
242 | } |
||
243 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.