• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 14603251033

22 Apr 2025 07:42PM UTC coverage: 84.377% (-0.02%) from 84.395%
14603251033

Pull #9528

github

web-flow
Merge 032578222 into e15078c66
Pull Request #9528: feat: add Time::addCalendarMonths() function

11 of 11 new or added lines in 1 file covered. (100.0%)

53 existing lines in 5 files now uncovered.

20853 of 24714 relevant lines covered (84.38%)

190.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.21
/system/Session/Session.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Session;
15

16
use CodeIgniter\Cookie\Cookie;
17
use CodeIgniter\I18n\Time;
18
use Config\Cookie as CookieConfig;
19
use Config\Session as SessionConfig;
20
use Psr\Log\LoggerAwareTrait;
21
use SessionHandlerInterface;
22

23
/**
24
 * Implementation of CodeIgniter session container.
25
 *
26
 * Session configuration is done through session variables and cookie related
27
 * variables in app/config/App.php
28
 *
29
 * @property string $session_id
30
 * @see \CodeIgniter\Session\SessionTest
31
 */
32
class Session implements SessionInterface
33
{
34
    use LoggerAwareTrait;
35

36
    /**
37
     * Instance of the driver to use.
38
     *
39
     * @var SessionHandlerInterface
40
     */
41
    protected $driver;
42

43
    /**
44
     * The storage driver to use: files, database, redis, memcached
45
     *
46
     * @var string
47
     *
48
     * @deprecated Use $this->config->driver.
49
     */
50
    protected $sessionDriverName;
51

52
    /**
53
     * The session cookie name, must contain only [0-9a-z_-] characters.
54
     *
55
     * @var string
56
     *
57
     * @deprecated Use $this->config->cookieName.
58
     */
59
    protected $sessionCookieName = 'ci_session';
60

61
    /**
62
     * The number of SECONDS you want the session to last.
63
     * Setting it to 0 (zero) means expire when the browser is closed.
64
     *
65
     * @var int
66
     *
67
     * @deprecated Use $this->config->expiration.
68
     */
69
    protected $sessionExpiration = 7200;
70

71
    /**
72
     * The location to save sessions to, driver dependent.
73
     *
74
     * For the 'files' driver, it's a path to a writable directory.
75
     * WARNING: Only absolute paths are supported!
76
     *
77
     * For the 'database' driver, it's a table name.
78
     *
79
     * @todo address memcache & redis needs
80
     *
81
     * IMPORTANT: You are REQUIRED to set a valid save path!
82
     *
83
     * @var string
84
     *
85
     * @deprecated Use $this->config->savePath.
86
     */
87
    protected $sessionSavePath;
88

89
    /**
90
     * Whether to match the user's IP address when reading the session data.
91
     *
92
     * WARNING: If you're using the database driver, don't forget to update
93
     * your session table's PRIMARY KEY when changing this setting.
94
     *
95
     * @var bool
96
     *
97
     * @deprecated Use $this->config->matchIP.
98
     */
99
    protected $sessionMatchIP = false;
100

101
    /**
102
     * How many seconds between CI regenerating the session ID.
103
     *
104
     * @var int
105
     *
106
     * @deprecated Use $this->config->timeToUpdate.
107
     */
108
    protected $sessionTimeToUpdate = 300;
109

110
    /**
111
     * Whether to destroy session data associated with the old session ID
112
     * when auto-regenerating the session ID. When set to FALSE, the data
113
     * will be later deleted by the garbage collector.
114
     *
115
     * @var bool
116
     *
117
     * @deprecated Use $this->config->regenerateDestroy.
118
     */
119
    protected $sessionRegenerateDestroy = false;
120

121
    /**
122
     * The session cookie instance.
123
     *
124
     * @var Cookie
125
     */
126
    protected $cookie;
127

128
    /**
129
     * The domain name to use for cookies.
130
     * Set to .your-domain.com for site-wide cookies.
131
     *
132
     * @var string
133
     *
134
     * @deprecated No longer used.
135
     */
136
    protected $cookieDomain = '';
137

138
    /**
139
     * Path used for storing cookies.
140
     * Typically will be a forward slash.
141
     *
142
     * @var string
143
     *
144
     * @deprecated No longer used.
145
     */
146
    protected $cookiePath = '/';
147

148
    /**
149
     * Cookie will only be set if a secure HTTPS connection exists.
150
     *
151
     * @var bool
152
     *
153
     * @deprecated No longer used.
154
     */
155
    protected $cookieSecure = false;
156

157
    /**
158
     * Cookie SameSite setting as described in RFC6265
159
     * Must be 'None', 'Lax' or 'Strict'.
160
     *
161
     * @var string
162
     *
163
     * @deprecated No longer used.
164
     */
165
    protected $cookieSameSite = Cookie::SAMESITE_LAX;
166

167
    /**
168
     * sid regex expression
169
     *
170
     * @var string
171
     */
172
    protected $sidRegexp;
173

174
    /**
175
     * Session Config
176
     */
177
    protected SessionConfig $config;
178

179
    /**
180
     * Constructor.
181
     *
182
     * Extract configuration settings and save them here.
183
     */
184
    public function __construct(SessionHandlerInterface $driver, SessionConfig $config)
185
    {
186
        $this->driver = $driver;
6,593✔
187

188
        $this->config = $config;
6,593✔
189

190
        $cookie = config(CookieConfig::class);
6,593✔
191

192
        $this->cookie = (new Cookie($this->config->cookieName, '', [
6,593✔
193
            'expires'  => $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration,
6,593✔
194
            'path'     => $cookie->path,
6,593✔
195
            'domain'   => $cookie->domain,
6,593✔
196
            'secure'   => $cookie->secure,
6,593✔
197
            'httponly' => true, // for security
6,593✔
198
            'samesite' => $cookie->samesite ?? Cookie::SAMESITE_LAX,
6,593✔
199
            'raw'      => $cookie->raw ?? false,
6,593✔
200
        ]))->withPrefix(''); // Cookie prefix should be ignored.
6,593✔
201

202
        helper('array');
6,593✔
203
    }
204

205
    /**
206
     * Initialize the session container and starts up the session.
207
     *
208
     * @return $this|null
209
     */
210
    public function start()
211
    {
212
        if (is_cli() && ENVIRONMENT !== 'testing') {
84✔
213
            // @codeCoverageIgnoreStart
214
            $this->logger->debug('Session: Initialization under CLI aborted.');
×
215

216
            return null;
×
217
            // @codeCoverageIgnoreEnd
218
        }
219

220
        if ((bool) ini_get('session.auto_start')) {
84✔
221
            $this->logger->error('Session: session.auto_start is enabled in php.ini. Aborting.');
×
222

223
            return null;
×
224
        }
225

226
        if (session_status() === PHP_SESSION_ACTIVE) {
84✔
227
            $this->logger->warning('Session: Sessions is enabled, and one exists. Please don\'t $session->start();');
×
228

229
            return null;
×
230
        }
231

232
        $this->configure();
84✔
233
        $this->setSaveHandler();
84✔
234

235
        // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
236
        if (
237
            isset($_COOKIE[$this->config->cookieName])
84✔
238
            && (! is_string($_COOKIE[$this->config->cookieName]) || preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$this->config->cookieName]) !== 1)
84✔
239
        ) {
240
            unset($_COOKIE[$this->config->cookieName]);
×
241
        }
242

243
        $this->startSession();
84✔
244

245
        // Is session ID auto-regeneration configured? (ignoring ajax requests)
246
        if ((! isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest')
84✔
247
            && ($regenerateTime = $this->config->timeToUpdate) > 0
84✔
248
        ) {
249
            if (! isset($_SESSION['__ci_last_regenerate'])) {
82✔
250
                $_SESSION['__ci_last_regenerate'] = Time::now()->getTimestamp();
81✔
251
            } elseif ($_SESSION['__ci_last_regenerate'] < (Time::now()->getTimestamp() - $regenerateTime)) {
3✔
252
                $this->regenerate($this->config->regenerateDestroy);
1✔
253
            }
254
        }
255
        // Another work-around ... PHP doesn't seem to send the session cookie
256
        // unless it is being currently created or regenerated
257
        elseif (isset($_COOKIE[$this->config->cookieName]) && $_COOKIE[$this->config->cookieName] === session_id()) {
2✔
258
            $this->setCookie();
×
259
        }
260

261
        $this->initVars();
84✔
262
        $this->logger->debug("Session: Class initialized using '" . $this->config->driver . "' driver.");
84✔
263

264
        return $this;
84✔
265
    }
266

267
    /**
268
     * Destroys the current session.
269
     *
270
     * @deprecated Use destroy() instead.
271
     *
272
     * @return void
273
     */
274
    public function stop()
275
    {
276
        $this->destroy();
×
277
    }
278

279
    /**
280
     * Configuration.
281
     *
282
     * Handle input binds and configuration defaults.
283
     *
284
     * @return void
285
     */
286
    protected function configure()
287
    {
288
        ini_set('session.name', $this->config->cookieName);
84✔
289

290
        $sameSite = $this->cookie->getSameSite() ?: ucfirst(Cookie::SAMESITE_LAX);
84✔
291

292
        $params = [
84✔
293
            'lifetime' => $this->config->expiration,
84✔
294
            'path'     => $this->cookie->getPath(),
84✔
295
            'domain'   => $this->cookie->getDomain(),
84✔
296
            'secure'   => $this->cookie->isSecure(),
84✔
297
            'httponly' => true, // HTTP only; Yes, this is intentional and not configurable for security reasons.
84✔
298
            'samesite' => $sameSite,
84✔
299
        ];
84✔
300

301
        ini_set('session.cookie_samesite', $sameSite);
84✔
302
        session_set_cookie_params($params);
84✔
303

304
        if ($this->config->expiration > 0) {
84✔
305
            ini_set('session.gc_maxlifetime', (string) $this->config->expiration);
83✔
306
        }
307

308
        if ($this->config->savePath !== '') {
84✔
309
            ini_set('session.save_path', $this->config->savePath);
40✔
310
        }
311

312
        // Security is king
313
        ini_set('session.use_trans_sid', '0');
84✔
314
        ini_set('session.use_strict_mode', '1');
84✔
315
        ini_set('session.use_cookies', '1');
84✔
316
        ini_set('session.use_only_cookies', '1');
84✔
317

318
        $this->configureSidLength();
84✔
319
    }
320

321
    /**
322
     * Configure session ID length
323
     *
324
     * To make life easier, we force the PHP defaults. Because PHP9 forces them.
325
     * See https://wiki.php.net/rfc/deprecations_php_8_4#sessionsid_length_and_sessionsid_bits_per_character
326
     *
327
     * @return void
328
     */
329
    protected function configureSidLength()
330
    {
331
        $bitsPerCharacter = (int) ini_get('session.sid_bits_per_character');
84✔
332
        $sidLength        = (int) ini_get('session.sid_length');
84✔
333

334
        // We force the PHP defaults.
335
        if (PHP_VERSION_ID < 90000) {
84✔
336
            if ($bitsPerCharacter !== 4) {
84✔
337
                ini_set('session.sid_bits_per_character', '4');
×
338
            }
339
            if ($sidLength !== 32) {
84✔
340
                ini_set('session.sid_length', '32');
×
341
            }
342
        }
343

344
        $this->sidRegexp = '[0-9a-f]{32}';
84✔
345
    }
346

347
    /**
348
     * Handle temporary variables
349
     *
350
     * Clears old "flash" data, marks the new one for deletion and handles
351
     * "temp" data deletion.
352
     *
353
     * @return void
354
     */
355
    protected function initVars()
356
    {
357
        if (! isset($_SESSION['__ci_vars'])) {
84✔
358
            return;
84✔
359
        }
360

361
        $currentTime = Time::now()->getTimestamp();
2✔
362

363
        foreach ($_SESSION['__ci_vars'] as $key => &$value) {
2✔
364
            if ($value === 'new') {
2✔
365
                $_SESSION['__ci_vars'][$key] = 'old';
2✔
366
            }
367
            // DO NOT move this above the 'new' check!
368
            elseif ($value === 'old' || $value < $currentTime) {
1✔
369
                unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
1✔
370
            }
371
        }
372

373
        if ($_SESSION['__ci_vars'] === []) {
2✔
374
            unset($_SESSION['__ci_vars']);
1✔
375
        }
376
    }
377

378
    /**
379
     * Regenerates the session ID.
380
     *
381
     * @param bool $destroy Should old session data be destroyed?
382
     *
383
     * @return void
384
     */
385
    public function regenerate(bool $destroy = false)
386
    {
387
        $_SESSION['__ci_last_regenerate'] = Time::now()->getTimestamp();
×
388
        session_regenerate_id($destroy);
×
389

390
        $this->removeOldSessionCookie();
×
391
    }
392

393
    private function removeOldSessionCookie(): void
394
    {
395
        $response              = service('response');
×
396
        $cookieStoreInResponse = $response->getCookieStore();
×
397

398
        if (! $cookieStoreInResponse->has($this->config->cookieName)) {
×
399
            return;
×
400
        }
401

402
        // CookieStore is immutable.
403
        $newCookieStore = $cookieStoreInResponse->remove($this->config->cookieName);
×
404

405
        // But clear() method clears cookies in the object (not immutable).
406
        $cookieStoreInResponse->clear();
×
407

408
        foreach ($newCookieStore as $cookie) {
×
409
            $response->setCookie($cookie);
×
410
        }
411
    }
412

413
    /**
414
     * Destroys the current session.
415
     *
416
     * @return void
417
     */
418
    public function destroy()
419
    {
420
        if (ENVIRONMENT === 'testing') {
×
421
            return;
×
422
        }
423

424
        session_destroy();
×
425
    }
426

427
    /**
428
     * Writes session data and close the current session.
429
     *
430
     * @return void
431
     */
432
    public function close()
433
    {
434
        if (ENVIRONMENT === 'testing') {
×
435
            return;
×
436
        }
437

438
        session_write_close();
×
439
    }
440

441
    /**
442
     * Sets user data into the session.
443
     *
444
     * If $data is a string, then it is interpreted as a session property
445
     * key, and  $value is expected to be non-null.
446
     *
447
     * If $data is an array, it is expected to be an array of key/value pairs
448
     * to be set as session properties.
449
     *
450
     * @param array<string, mixed>|list<string>|string $data  Property name or associative array of properties
451
     * @param mixed                                    $value Property value if single key provided
452
     *
453
     * @return void
454
     */
455
    public function set($data, $value = null)
456
    {
457
        $data = is_array($data) ? $data : [$data => $value];
117✔
458

459
        if (array_is_list($data)) {
117✔
460
            $data = array_fill_keys($data, null);
1✔
461
        }
462

463
        foreach ($data as $sessionKey => $sessionValue) {
117✔
464
            $_SESSION[$sessionKey] = $sessionValue;
117✔
465
        }
466
    }
467

468
    /**
469
     * Get user data that has been set in the session.
470
     *
471
     * If the property exists as "normal", returns it.
472
     * Otherwise, returns an array of any temp or flash data values with the
473
     * property key.
474
     *
475
     * Replaces the legacy method $session->userdata();
476
     *
477
     * @param string|null $key Identifier of the session property to retrieve
478
     *
479
     * @return ($key is string ? mixed : array<string, mixed>)
480
     */
481
    public function get(?string $key = null)
482
    {
483
        if (! isset($_SESSION) || $_SESSION === []) {
67✔
484
            return $key === null ? [] : null;
12✔
485
        }
486

487
        $key ??= '';
57✔
488

489
        if ($key !== '') {
57✔
490
            return $_SESSION[$key] ?? dot_array_search($key, $_SESSION);
56✔
491
        }
492

493
        $userdata = [];
1✔
494
        $exclude  = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys());
1✔
495

496
        foreach (array_keys($_SESSION) as $key) {
1✔
497
            if (! in_array($key, $exclude, true)) {
1✔
498
                $userdata[$key] = $_SESSION[$key];
1✔
499
            }
500
        }
501

502
        return $userdata;
1✔
503
    }
504

505
    /**
506
     * Returns whether an index exists in the session array.
507
     *
508
     * @param string $key Identifier of the session property we are interested in.
509
     */
510
    public function has(string $key): bool
511
    {
512
        return isset($_SESSION[$key]);
38✔
513
    }
514

515
    /**
516
     * Push new value onto session value that is array.
517
     *
518
     * @param string               $key  Identifier of the session property we are interested in.
519
     * @param array<string, mixed> $data value to be pushed to existing session key.
520
     *
521
     * @return void
522
     */
523
    public function push(string $key, array $data)
524
    {
525
        if ($this->has($key) && is_array($value = $this->get($key))) {
1✔
526
            $this->set($key, array_merge($value, $data));
1✔
527
        }
528
    }
529

530
    /**
531
     * Remove one or more session properties.
532
     *
533
     * If $key is an array, it is interpreted as an array of string property
534
     * identifiers to remove. Otherwise, it is interpreted as the identifier
535
     * of a specific session property to remove.
536
     *
537
     * @param list<string>|string $key Identifier of the session property or properties to remove.
538
     *
539
     * @return void
540
     */
541
    public function remove($key)
542
    {
543
        $key = is_array($key) ? $key : [$key];
2✔
544

545
        foreach ($key as $k) {
2✔
546
            unset($_SESSION[$k]);
2✔
547
        }
548
    }
549

550
    /**
551
     * Magic method to set variables in the session by simply calling
552
     *  $session->foo = bar;
553
     *
554
     * @param string $key   Identifier of the session property to set.
555
     * @param mixed  $value
556
     *
557
     * @return void
558
     */
559
    public function __set(string $key, $value)
560
    {
561
        $_SESSION[$key] = $value;
1✔
562
    }
563

564
    /**
565
     * Magic method to get session variables by simply calling
566
     *  $foo = $session->foo;
567
     *
568
     * @param string $key Identifier of the session property to remove.
569
     *
570
     * @return mixed
571
     */
572
    public function __get(string $key)
573
    {
574
        // Note: Keep this order the same, just in case somebody wants to
575
        // use 'session_id' as a session data key, for whatever reason
576
        if (isset($_SESSION[$key])) {
1✔
577
            return $_SESSION[$key];
1✔
578
        }
579

UNCOV
580
        if ($key === 'session_id') {
×
UNCOV
581
            return session_id();
×
582
        }
583

UNCOV
584
        return null;
×
585
    }
586

587
    /**
588
     * Magic method to check for session variables.
589
     *
590
     * Different from `has()` in that it will validate 'session_id' as well.
591
     * Mostly used by internal PHP functions, users should stick to `has()`.
592
     *
593
     * @param string $key Identifier of the session property to remove.
594
     */
595
    public function __isset(string $key): bool
596
    {
597
        return isset($_SESSION[$key]) || $key === 'session_id';
2✔
598
    }
599

600
    /**
601
     * Sets data into the session that will only last for a single request.
602
     * Perfect for use with single-use status update messages.
603
     *
604
     * If $data is an array, it is interpreted as an associative array of
605
     * key/value pairs for flashdata properties.
606
     * Otherwise, it is interpreted as the identifier of a specific
607
     * flashdata property, with $value containing the property value.
608
     *
609
     * @param array<string, mixed>|string $data  Property identifier or associative array of properties
610
     * @param mixed                       $value Property value if $data is a scalar
611
     *
612
     * @return void
613
     */
614
    public function setFlashdata($data, $value = null)
615
    {
616
        $this->set($data, $value);
11✔
617
        $this->markAsFlashdata(is_array($data) ? array_keys($data) : $data);
11✔
618
    }
619

620
    /**
621
     * Retrieve one or more items of flash data from the session.
622
     *
623
     * If the item key is null, return all flashdata.
624
     *
625
     * @param string|null $key Property identifier
626
     *
627
     * @return ($key is string ? mixed : array<string, mixed>)
628
     */
629
    public function getFlashdata(?string $key = null)
630
    {
UNCOV
631
        $_SESSION['__ci_vars'] ??= [];
×
632

UNCOV
633
        if (isset($key)) {
×
UNCOV
634
            if (! isset($_SESSION['__ci_vars'][$key]) || is_int($_SESSION['__ci_vars'][$key])) {
×
UNCOV
635
                return null;
×
636
            }
637

UNCOV
638
            return $_SESSION[$key] ?? null;
×
639
        }
640

641
        $flashdata = [];
×
642

UNCOV
643
        foreach ($_SESSION['__ci_vars'] as $key => $value) {
×
UNCOV
644
            if (! is_int($value)) {
×
645
                $flashdata[$key] = $_SESSION[$key];
×
646
            }
647
        }
648

649
        return $flashdata;
×
650
    }
651

652
    /**
653
     * Keeps a single piece of flash data alive for one more request.
654
     *
655
     * @param list<string>|string $key Property identifier or array of them
656
     *
657
     * @return void
658
     */
659
    public function keepFlashdata($key)
660
    {
661
        $this->markAsFlashdata($key);
1✔
662
    }
663

664
    /**
665
     * Mark a session property or properties as flashdata. This returns
666
     * `false` if any of the properties were not already set.
667
     *
668
     * @param list<string>|string $key Property identifier or array of them
669
     */
670
    public function markAsFlashdata($key): bool
671
    {
672
        $keys = is_array($key) ? $key : [$key];
12✔
673

674
        foreach ($keys as $sessionKey) {
12✔
675
            if (! isset($_SESSION[$sessionKey])) {
12✔
676
                return false;
1✔
677
            }
678
        }
679

680
        $_SESSION['__ci_vars'] ??= [];
11✔
681
        $_SESSION['__ci_vars'] = [...$_SESSION['__ci_vars'], ...array_fill_keys($keys, 'new')];
11✔
682

683
        return true;
11✔
684
    }
685

686
    /**
687
     * Unmark data in the session as flashdata.
688
     *
689
     * @param list<string>|string $key Property identifier or array of them
690
     *
691
     * @return void
692
     */
693
    public function unmarkFlashdata($key)
694
    {
695
        if (! isset($_SESSION['__ci_vars'])) {
1✔
UNCOV
696
            return;
×
697
        }
698

699
        if (! is_array($key)) {
1✔
700
            $key = [$key];
1✔
701
        }
702

703
        foreach ($key as $k) {
1✔
704
            if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) {
1✔
705
                unset($_SESSION['__ci_vars'][$k]);
1✔
706
            }
707
        }
708

709
        if ($_SESSION['__ci_vars'] === []) {
1✔
710
            unset($_SESSION['__ci_vars']);
1✔
711
        }
712
    }
713

714
    /**
715
     * Retrieve all of the keys for session data marked as flashdata.
716
     *
717
     * @return list<string>
718
     */
719
    public function getFlashKeys(): array
720
    {
721
        if (! isset($_SESSION['__ci_vars'])) {
2✔
722
            return [];
1✔
723
        }
724

725
        $keys = [];
1✔
726

727
        foreach (array_keys($_SESSION['__ci_vars']) as $key) {
1✔
728
            if (! is_int($_SESSION['__ci_vars'][$key])) {
1✔
729
                $keys[] = $key;
1✔
730
            }
731
        }
732

733
        return $keys;
1✔
734
    }
735

736
    /**
737
     * Sets new data into the session, and marks it as temporary data
738
     * with a set lifespan.
739
     *
740
     * @param array<string, mixed>|list<string>|string $data  Session data key or associative array of items
741
     * @param mixed                                    $value Value to store
742
     * @param int                                      $ttl   Time-to-live in seconds
743
     *
744
     * @return void
745
     */
746
    public function setTempdata($data, $value = null, int $ttl = 300)
747
    {
748
        $this->set($data, $value);
10✔
749
        $this->markAsTempdata($data, $ttl);
10✔
750
    }
751

752
    /**
753
     * Returns either a single piece of tempdata, or all temp data currently
754
     * in the session.
755
     *
756
     * @param string|null $key Session data key
757
     *
758
     * @return ($key is string ? mixed : array<string, mixed>)
759
     */
760
    public function getTempdata(?string $key = null)
761
    {
762
        $_SESSION['__ci_vars'] ??= [];
5✔
763

764
        if (isset($key)) {
5✔
765
            if (! isset($_SESSION['__ci_vars'][$key]) || ! is_int($_SESSION['__ci_vars'][$key])) {
1✔
UNCOV
766
                return null;
×
767
            }
768

769
            return $_SESSION[$key] ?? null;
1✔
770
        }
771

772
        $tempdata = [];
4✔
773

774
        foreach ($_SESSION['__ci_vars'] as $key => $value) {
4✔
775
            if (is_int($value)) {
3✔
776
                $tempdata[$key] = $_SESSION[$key];
3✔
777
            }
778
        }
779

780
        return $tempdata;
4✔
781
    }
782

783
    /**
784
     * Removes a single piece of temporary data from the session.
785
     *
786
     * @param string $key Session data key
787
     *
788
     * @return void
789
     */
790
    public function removeTempdata(string $key)
791
    {
792
        $this->unmarkTempdata($key);
1✔
793
        unset($_SESSION[$key]);
1✔
794
    }
795

796
    /**
797
     * Mark one of more pieces of data as being temporary, meaning that
798
     * it has a set lifespan within the session.
799
     *
800
     * Returns `false` if any of the properties were not set.
801
     *
802
     * @param array<string, mixed>|list<string>|string $key Property identifier or array of them
803
     * @param int                                      $ttl Time to live, in seconds
804
     */
805
    public function markAsTempdata($key, int $ttl = 300): bool
806
    {
807
        $time = Time::now()->getTimestamp();
11✔
808
        $keys = is_array($key) ? $key : [$key];
11✔
809

810
        if (array_is_list($keys)) {
11✔
811
            $keys = array_fill_keys($keys, $ttl);
4✔
812
        }
813

814
        $tempdata = [];
11✔
815

816
        foreach ($keys as $sessionKey => $timeToLive) {
11✔
817
            if (! array_key_exists($sessionKey, $_SESSION)) {
11✔
818
                return false;
1✔
819
            }
820

821
            if (is_int($timeToLive)) {
11✔
822
                $timeToLive += $time;
5✔
823
            } else {
824
                $timeToLive = $time + $ttl;
7✔
825
            }
826

827
            $tempdata[$sessionKey] = $timeToLive;
11✔
828
        }
829

830
        $_SESSION['__ci_vars'] ??= [];
10✔
831
        $_SESSION['__ci_vars'] = [...$_SESSION['__ci_vars'], ...$tempdata];
10✔
832

833
        return true;
10✔
834
    }
835

836
    /**
837
     * Unmarks temporary data in the session, effectively removing its
838
     * lifespan and allowing it to live as long as the session does.
839
     *
840
     * @param list<string>|string $key Property identifier or array of them
841
     *
842
     * @return void
843
     */
844
    public function unmarkTempdata($key)
845
    {
846
        if (! isset($_SESSION['__ci_vars'])) {
3✔
UNCOV
847
            return;
×
848
        }
849

850
        if (! is_array($key)) {
3✔
851
            $key = [$key];
2✔
852
        }
853

854
        foreach ($key as $k) {
3✔
855
            if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) {
3✔
856
                unset($_SESSION['__ci_vars'][$k]);
3✔
857
            }
858
        }
859

860
        if ($_SESSION['__ci_vars'] === []) {
3✔
861
            unset($_SESSION['__ci_vars']);
1✔
862
        }
863
    }
864

865
    /**
866
     * Retrieve the keys of all session data that have been marked as temporary data.
867
     *
868
     * @return list<string>
869
     */
870
    public function getTempKeys(): array
871
    {
872
        if (! isset($_SESSION['__ci_vars'])) {
2✔
873
            return [];
1✔
874
        }
875

876
        $keys = [];
1✔
877

878
        foreach (array_keys($_SESSION['__ci_vars']) as $key) {
1✔
879
            if (is_int($_SESSION['__ci_vars'][$key])) {
1✔
880
                $keys[] = $key;
1✔
881
            }
882
        }
883

884
        return $keys;
1✔
885
    }
886

887
    /**
888
     * Sets the driver as the session handler in PHP.
889
     * Extracted for easier testing.
890
     *
891
     * @return void
892
     */
893
    protected function setSaveHandler()
894
    {
895
        session_set_save_handler($this->driver, true);
40✔
896
    }
897

898
    /**
899
     * Starts the session.
900
     * Extracted for testing reasons.
901
     *
902
     * @return void
903
     */
904
    protected function startSession()
905
    {
906
        if (ENVIRONMENT === 'testing') {
40✔
907
            $_SESSION = [];
40✔
908

909
            return;
40✔
910
        }
911

UNCOV
912
        session_start(); // @codeCoverageIgnore
×
913
    }
914

915
    /**
916
     * Takes care of setting the cookie on the client side.
917
     *
918
     * @codeCoverageIgnore
919
     *
920
     * @return void
921
     */
922
    protected function setCookie()
923
    {
UNCOV
924
        $expiration   = $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration;
×
UNCOV
925
        $this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
×
926

UNCOV
927
        $response = service('response');
×
UNCOV
928
        $response->setCookie($this->cookie);
×
929
    }
930
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc