@@ -43,13 +43,17 @@ class Session implements SessionInterface
43
43
* The storage driver to use: files, database, redis, memcached
44
44
*
45
45
* @var string
46
+ *
47
+ * @deprecated Use $this->config->driver.
46
48
*/
47
49
protected $ sessionDriverName ;
48
50
49
51
/**
50
52
* The session cookie name, must contain only [0-9a-z_-] characters.
51
53
*
52
54
* @var string
55
+ *
56
+ * @deprecated Use $this->config->cookieName.
53
57
*/
54
58
protected $ sessionCookieName = 'ci_session ' ;
55
59
@@ -58,11 +62,13 @@ class Session implements SessionInterface
58
62
* Setting it to 0 (zero) means expire when the browser is closed.
59
63
*
60
64
* @var int
65
+ *
66
+ * @deprecated Use $this->config->expiration.
61
67
*/
62
68
protected $ sessionExpiration = 7200 ;
63
69
64
70
/**
65
- * The location to save sessions to, driver dependent..
71
+ * The location to save sessions to, driver dependent.
66
72
*
67
73
* For the 'files' driver, it's a path to a writable directory.
68
74
* WARNING: Only absolute paths are supported!
@@ -74,6 +80,8 @@ class Session implements SessionInterface
74
80
* IMPORTANT: You are REQUIRED to set a valid save path!
75
81
*
76
82
* @var string
83
+ *
84
+ * @deprecated Use $this->config->savePath.
77
85
*/
78
86
protected $ sessionSavePath ;
79
87
@@ -84,13 +92,17 @@ class Session implements SessionInterface
84
92
* your session table's PRIMARY KEY when changing this setting.
85
93
*
86
94
* @var bool
95
+ *
96
+ * @deprecated Use $this->config->matchIP.
87
97
*/
88
98
protected $ sessionMatchIP = false ;
89
99
90
100
/**
91
101
* How many seconds between CI regenerating the session ID.
92
102
*
93
103
* @var int
104
+ *
105
+ * @deprecated Use $this->config->timeToUpdate.
94
106
*/
95
107
protected $ sessionTimeToUpdate = 300 ;
96
108
@@ -100,6 +112,8 @@ class Session implements SessionInterface
100
112
* will be later deleted by the garbage collector.
101
113
*
102
114
* @var bool
115
+ *
116
+ * @deprecated Use $this->config->regenerateDestroy.
103
117
*/
104
118
protected $ sessionRegenerateDestroy = false ;
105
119
@@ -156,6 +170,11 @@ class Session implements SessionInterface
156
170
*/
157
171
protected $ sidRegexp ;
158
172
173
+ /**
174
+ * Session Config
175
+ */
176
+ protected SessionConfig $ config ;
177
+
159
178
/**
160
179
* Constructor.
161
180
*
@@ -165,14 +184,16 @@ public function __construct(SessionHandlerInterface $driver, SessionConfig $sess
165
184
{
166
185
$ this ->driver = $ driver ;
167
186
187
+ $ this ->config = $ session ;
188
+
168
189
// Store Session configurations
169
190
$ this ->sessionDriverName = $ session ->driver ;
170
- $ this ->sessionCookieName = $ session ->cookieName ?? $ this -> sessionCookieName ;
171
- $ this ->sessionExpiration = $ session ->expiration ?? $ this -> sessionExpiration ;
191
+ $ this ->sessionCookieName = $ session ->cookieName ;
192
+ $ this ->sessionExpiration = $ session ->expiration ;
172
193
$ this ->sessionSavePath = $ session ->savePath ;
173
- $ this ->sessionMatchIP = $ session ->matchIP ?? $ this -> sessionMatchIP ;
174
- $ this ->sessionTimeToUpdate = $ session ->timeToUpdate ?? $ this -> sessionTimeToUpdate ;
175
- $ this ->sessionRegenerateDestroy = $ session ->regenerateDestroy ?? $ this -> sessionRegenerateDestroy ;
194
+ $ this ->sessionMatchIP = $ session ->matchIP ;
195
+ $ this ->sessionTimeToUpdate = $ session ->timeToUpdate ;
196
+ $ this ->sessionRegenerateDestroy = $ session ->regenerateDestroy ;
176
197
177
198
/** @var App $config */
178
199
$ config = config ('App ' );
@@ -186,8 +207,8 @@ public function __construct(SessionHandlerInterface $driver, SessionConfig $sess
186
207
/** @var CookieConfig|null $cookie */
187
208
$ cookie = config ('Cookie ' );
188
209
189
- $ this ->cookie = (new Cookie ($ this ->sessionCookieName , '' , [
190
- 'expires ' => $ this ->sessionExpiration === 0 ? 0 : Time::now ()->getTimestamp () + $ this ->sessionExpiration ,
210
+ $ this ->cookie = (new Cookie ($ this ->config -> cookieName , '' , [
211
+ 'expires ' => $ this ->config -> expiration === 0 ? 0 : Time::now ()->getTimestamp () + $ this ->config -> expiration ,
191
212
'path ' => $ cookie ->path ?? $ config ->cookiePath ,
192
213
'domain ' => $ cookie ->domain ?? $ config ->cookieDomain ,
193
214
'secure ' => $ cookie ->secure ?? $ config ->cookieSecure ,
@@ -230,32 +251,32 @@ public function start()
230
251
$ this ->setSaveHandler ();
231
252
232
253
// Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
233
- if (isset ($ _COOKIE [$ this ->sessionCookieName ])
234
- && (! is_string ($ _COOKIE [$ this ->sessionCookieName ]) || ! preg_match ('#\A ' . $ this ->sidRegexp . '\z# ' , $ _COOKIE [$ this ->sessionCookieName ]))
254
+ if (isset ($ _COOKIE [$ this ->config -> cookieName ])
255
+ && (! is_string ($ _COOKIE [$ this ->config -> cookieName ]) || ! preg_match ('#\A ' . $ this ->sidRegexp . '\z# ' , $ _COOKIE [$ this ->config -> cookieName ]))
235
256
) {
236
- unset($ _COOKIE [$ this ->sessionCookieName ]);
257
+ unset($ _COOKIE [$ this ->config -> cookieName ]);
237
258
}
238
259
239
260
$ this ->startSession ();
240
261
241
262
// Is session ID auto-regeneration configured? (ignoring ajax requests)
242
263
if ((empty ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) || strtolower ($ _SERVER ['HTTP_X_REQUESTED_WITH ' ]) !== 'xmlhttprequest ' )
243
- && ($ regenerateTime = $ this ->sessionTimeToUpdate ) > 0
264
+ && ($ regenerateTime = $ this ->config -> timeToUpdate ) > 0
244
265
) {
245
266
if (! isset ($ _SESSION ['__ci_last_regenerate ' ])) {
246
267
$ _SESSION ['__ci_last_regenerate ' ] = Time::now ()->getTimestamp ();
247
268
} elseif ($ _SESSION ['__ci_last_regenerate ' ] < (Time::now ()->getTimestamp () - $ regenerateTime )) {
248
- $ this ->regenerate ((bool ) $ this ->sessionRegenerateDestroy );
269
+ $ this ->regenerate ((bool ) $ this ->config -> regenerateDestroy );
249
270
}
250
271
}
251
272
// Another work-around ... PHP doesn't seem to send the session cookie
252
273
// unless it is being currently created or regenerated
253
- elseif (isset ($ _COOKIE [$ this ->sessionCookieName ]) && $ _COOKIE [$ this ->sessionCookieName ] === session_id ()) {
274
+ elseif (isset ($ _COOKIE [$ this ->config -> cookieName ]) && $ _COOKIE [$ this ->config -> cookieName ] === session_id ()) {
254
275
$ this ->setCookie ();
255
276
}
256
277
257
278
$ this ->initVars ();
258
- $ this ->logger ->info ("Session: Class initialized using ' " . $ this ->sessionDriverName . "' driver. " );
279
+ $ this ->logger ->info ("Session: Class initialized using ' " . $ this ->config -> driver . "' driver. " );
259
280
260
281
return $ this ;
261
282
}
@@ -270,7 +291,7 @@ public function start()
270
291
public function stop ()
271
292
{
272
293
setcookie (
273
- $ this ->sessionCookieName ,
294
+ $ this ->config -> cookieName ,
274
295
session_id (),
275
296
['expires ' => 1 , 'path ' => $ this ->cookie ->getPath (), 'domain ' => $ this ->cookie ->getDomain (), 'secure ' => $ this ->cookie ->isSecure (), 'httponly ' => true ]
276
297
);
@@ -285,16 +306,12 @@ public function stop()
285
306
*/
286
307
protected function configure ()
287
308
{
288
- if (empty ($ this ->sessionCookieName )) {
289
- $ this ->sessionCookieName = ini_get ('session.name ' );
290
- } else {
291
- ini_set ('session.name ' , $ this ->sessionCookieName );
292
- }
309
+ ini_set ('session.name ' , $ this ->config ->cookieName );
293
310
294
311
$ sameSite = $ this ->cookie ->getSameSite () ?: ucfirst (Cookie::SAMESITE_LAX );
295
312
296
313
$ params = [
297
- 'lifetime ' => $ this ->sessionExpiration ,
314
+ 'lifetime ' => $ this ->config -> expiration ,
298
315
'path ' => $ this ->cookie ->getPath (),
299
316
'domain ' => $ this ->cookie ->getDomain (),
300
317
'secure ' => $ this ->cookie ->isSecure (),
@@ -305,14 +322,12 @@ protected function configure()
305
322
ini_set ('session.cookie_samesite ' , $ sameSite );
306
323
session_set_cookie_params ($ params );
307
324
308
- if (! isset ($ this ->sessionExpiration )) {
309
- $ this ->sessionExpiration = (int ) ini_get ('session.gc_maxlifetime ' );
310
- } elseif ($ this ->sessionExpiration > 0 ) {
311
- ini_set ('session.gc_maxlifetime ' , (string ) $ this ->sessionExpiration );
325
+ if ($ this ->config ->expiration > 0 ) {
326
+ ini_set ('session.gc_maxlifetime ' , (string ) $ this ->config ->expiration );
312
327
}
313
328
314
- if (! empty ($ this ->sessionSavePath )) {
315
- ini_set ('session.save_path ' , $ this ->sessionSavePath );
329
+ if (! empty ($ this ->config -> savePath )) {
330
+ ini_set ('session.save_path ' , $ this ->config -> savePath );
316
331
}
317
332
318
333
// Security is king
@@ -419,12 +434,12 @@ private function removeOldSessionCookie(): void
419
434
$ response = Services::response ();
420
435
$ cookieStoreInResponse = $ response ->getCookieStore ();
421
436
422
- if (! $ cookieStoreInResponse ->has ($ this ->sessionCookieName )) {
437
+ if (! $ cookieStoreInResponse ->has ($ this ->config -> cookieName )) {
423
438
return ;
424
439
}
425
440
426
441
// CookieStore is immutable.
427
- $ newCookieStore = $ cookieStoreInResponse ->remove ($ this ->sessionCookieName );
442
+ $ newCookieStore = $ cookieStoreInResponse ->remove ($ this ->config -> cookieName );
428
443
429
444
// But clear() method clears cookies in the object (not immutable).
430
445
$ cookieStoreInResponse ->clear ();
@@ -924,7 +939,7 @@ protected function startSession()
924
939
*/
925
940
protected function setCookie ()
926
941
{
927
- $ expiration = $ this ->sessionExpiration === 0 ? 0 : Time::now ()->getTimestamp () + $ this ->sessionExpiration ;
942
+ $ expiration = $ this ->config -> expiration === 0 ? 0 : Time::now ()->getTimestamp () + $ this ->config -> expiration ;
928
943
$ this ->cookie = $ this ->cookie ->withValue (session_id ())->withExpires ($ expiration );
929
944
930
945
$ response = Services::response ();
0 commit comments