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

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

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

75.31
/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,362✔
187

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

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

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

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

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

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

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

223
            return;
×
224
        }
225

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

229
            return;
×
230
        }
231

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

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

242
        $this->startSession();
80✔
243

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

260
        $this->initVars();
80✔
261
        $this->logger->info("Session: Class initialized using '" . $this->config->driver . "' driver.");
80✔
262

263
        return $this;
80✔
264
    }
265

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

276
    /**
277
     * Configuration.
278
     *
279
     * Handle input binds and configuration defaults.
280
     */
281
    protected function configure()
282
    {
283
        ini_set('session.name', $this->config->cookieName);
80✔
284

285
        $sameSite = $this->cookie->getSameSite() ?: ucfirst(Cookie::SAMESITE_LAX);
80✔
286

287
        $params = [
80✔
288
            'lifetime' => $this->config->expiration,
80✔
289
            'path'     => $this->cookie->getPath(),
80✔
290
            'domain'   => $this->cookie->getDomain(),
80✔
291
            'secure'   => $this->cookie->isSecure(),
80✔
292
            'httponly' => true, // HTTP only; Yes, this is intentional and not configurable for security reasons.
80✔
293
            'samesite' => $sameSite,
80✔
294
        ];
80✔
295

296
        ini_set('session.cookie_samesite', $sameSite);
80✔
297
        session_set_cookie_params($params);
80✔
298

299
        if ($this->config->expiration > 0) {
80✔
300
            ini_set('session.gc_maxlifetime', (string) $this->config->expiration);
79✔
301
        }
302

303
        if ($this->config->savePath !== '') {
80✔
304
            ini_set('session.save_path', $this->config->savePath);
39✔
305
        }
306

307
        // Security is king
308
        ini_set('session.use_trans_sid', '0');
80✔
309
        ini_set('session.use_strict_mode', '1');
80✔
310
        ini_set('session.use_cookies', '1');
80✔
311
        ini_set('session.use_only_cookies', '1');
80✔
312

313
        $this->configureSidLength();
80✔
314
    }
315

316
    /**
317
     * Configure session ID length
318
     *
319
     * To make life easier, we used to force SHA-1 and 4 bits per
320
     * character on everyone. And of course, someone was unhappy.
321
     *
322
     * Then PHP 7.1 broke backwards-compatibility because ext/session
323
     * is such a mess that nobody wants to touch it with a pole stick,
324
     * and the one guy who does, nobody has the energy to argue with.
325
     *
326
     * So we were forced to make changes, and OF COURSE something was
327
     * going to break and now we have this pile of shit. -- Narf
328
     */
329
    protected function configureSidLength()
330
    {
331
        $bitsPerCharacter = (int) (ini_get('session.sid_bits_per_character') !== false
80✔
332
            ? ini_get('session.sid_bits_per_character')
80✔
333
            : 4);
×
334

335
        $sidLength = (int) (ini_get('session.sid_length') !== false
80✔
336
            ? ini_get('session.sid_length')
80✔
337
            : 40);
×
338

339
        if (($sidLength * $bitsPerCharacter) < 160) {
80✔
340
            $bits = ($sidLength * $bitsPerCharacter);
×
341
            // Add as many more characters as necessary to reach at least 160 bits
342
            $sidLength += (int) ceil((160 % $bits) / $bitsPerCharacter);
×
343
            ini_set('session.sid_length', (string) $sidLength);
×
344
        }
345

346
        // Yes, 4,5,6 are the only known possible values as of 2016-10-27
347
        switch ($bitsPerCharacter) {
348
            case 4:
80✔
349
                $this->sidRegexp = '[0-9a-f]';
×
350
                break;
×
351

352
            case 5:
80✔
353
                $this->sidRegexp = '[0-9a-v]';
80✔
354
                break;
80✔
355

356
            case 6:
×
357
                $this->sidRegexp = '[0-9a-zA-Z,-]';
×
358
                break;
×
359
        }
360

361
        $this->sidRegexp .= '{' . $sidLength . '}';
80✔
362
    }
363

364
    /**
365
     * Handle temporary variables
366
     *
367
     * Clears old "flash" data, marks the new one for deletion and handles
368
     * "temp" data deletion.
369
     */
370
    protected function initVars()
371
    {
372
        if (! isset($_SESSION['__ci_vars'])) {
80✔
373
            return;
80✔
374
        }
375

376
        $currentTime = Time::now()->getTimestamp();
2✔
377

378
        foreach ($_SESSION['__ci_vars'] as $key => &$value) {
2✔
379
            if ($value === 'new') {
2✔
380
                $_SESSION['__ci_vars'][$key] = 'old';
2✔
381
            }
382
            // DO NOT move this above the 'new' check!
383
            elseif ($value === 'old' || $value < $currentTime) {
1✔
384
                unset($_SESSION[$key], $_SESSION['__ci_vars'][$key]);
1✔
385
            }
386
        }
387

388
        if ($_SESSION['__ci_vars'] === []) {
2✔
389
            unset($_SESSION['__ci_vars']);
1✔
390
        }
391
    }
392

393
    /**
394
     * Regenerates the session ID.
395
     *
396
     * @param bool $destroy Should old session data be destroyed?
397
     */
398
    public function regenerate(bool $destroy = false)
399
    {
400
        $_SESSION['__ci_last_regenerate'] = Time::now()->getTimestamp();
×
401
        session_regenerate_id($destroy);
×
402

403
        $this->removeOldSessionCookie();
×
404
    }
405

406
    private function removeOldSessionCookie(): void
407
    {
408
        $response              = service('response');
×
409
        $cookieStoreInResponse = $response->getCookieStore();
×
410

411
        if (! $cookieStoreInResponse->has($this->config->cookieName)) {
×
412
            return;
×
413
        }
414

415
        // CookieStore is immutable.
416
        $newCookieStore = $cookieStoreInResponse->remove($this->config->cookieName);
×
417

418
        // But clear() method clears cookies in the object (not immutable).
419
        $cookieStoreInResponse->clear();
×
420

421
        foreach ($newCookieStore as $cookie) {
×
422
            $response->setCookie($cookie);
×
423
        }
424
    }
425

426
    /**
427
     * Destroys the current session.
428
     */
429
    public function destroy()
430
    {
431
        if (ENVIRONMENT === 'testing') {
×
432
            return;
×
433
        }
434

435
        session_destroy();
×
436
    }
437

438
    /**
439
     * Writes session data and close the current session.
440
     *
441
     * @return void
442
     */
443
    public function close()
444
    {
445
        if (ENVIRONMENT === 'testing') {
×
446
            return;
×
447
        }
448

449
        session_write_close();
×
450
    }
451

452
    /**
453
     * Sets user data into the session.
454
     *
455
     * If $data is a string, then it is interpreted as a session property
456
     * key, and  $value is expected to be non-null.
457
     *
458
     * If $data is an array, it is expected to be an array of key/value pairs
459
     * to be set as session properties.
460
     *
461
     * @param array|string                            $data  Property name or associative array of properties
462
     * @param array|bool|float|int|object|string|null $value Property value if single key provided
463
     */
464
    public function set($data, $value = null)
465
    {
466
        if (is_array($data)) {
113✔
467
            foreach ($data as $key => &$value) {
12✔
468
                if (is_int($key)) {
12✔
469
                    $_SESSION[$value] = null;
1✔
470
                } else {
471
                    $_SESSION[$key] = $value;
11✔
472
                }
473
            }
474

475
            return;
12✔
476
        }
477

478
        $_SESSION[$data] = $value;
103✔
479
    }
480

481
    /**
482
     * Get user data that has been set in the session.
483
     *
484
     * If the property exists as "normal", returns it.
485
     * Otherwise, returns an array of any temp or flash data values with the
486
     * property key.
487
     *
488
     * Replaces the legacy method $session->userdata();
489
     *
490
     * @param non-empty-string|null $key Identifier of the session property to retrieve
491
     *
492
     * @return array|bool|float|int|object|string|null The property value(s)
493
     */
494
    public function get(?string $key = null)
495
    {
496
        if ($key !== null && $key !== '' && (null !== ($value = $_SESSION[$key] ?? null) || null !== ($value = dot_array_search($key, $_SESSION ?? [])))) {
67✔
497
            return $value;
51✔
498
        }
499

500
        if (! isset($_SESSION) || $_SESSION === []) {
18✔
501
            return $key === null ? [] : null;
12✔
502
        }
503

504
        if ($key !== null && $key !== '') {
6✔
505
            return null;
5✔
506
        }
507

508
        $userdata = [];
1✔
509
        $_exclude = array_merge(['__ci_vars'], $this->getFlashKeys(), $this->getTempKeys());
1✔
510

511
        $keys = array_keys($_SESSION);
1✔
512

513
        foreach ($keys as $key) {
1✔
514
            if (! in_array($key, $_exclude, true)) {
1✔
515
                $userdata[$key] = $_SESSION[$key];
1✔
516
            }
517
        }
518

519
        return $userdata;
1✔
520
    }
521

522
    /**
523
     * Returns whether an index exists in the session array.
524
     *
525
     * @param string $key Identifier of the session property we are interested in.
526
     */
527
    public function has(string $key): bool
528
    {
529
        return isset($_SESSION[$key]);
38✔
530
    }
531

532
    /**
533
     * Push new value onto session value that is array.
534
     *
535
     * @param string $key  Identifier of the session property we are interested in.
536
     * @param array  $data value to be pushed to existing session key.
537
     */
538
    public function push(string $key, array $data)
539
    {
540
        if ($this->has($key) && is_array($value = $this->get($key))) {
1✔
541
            $this->set($key, array_merge($value, $data));
1✔
542
        }
543
    }
544

545
    /**
546
     * Remove one or more session properties.
547
     *
548
     * If $key is an array, it is interpreted as an array of string property
549
     * identifiers to remove. Otherwise, it is interpreted as the identifier
550
     * of a specific session property to remove.
551
     *
552
     * @param array|string $key Identifier of the session property or properties to remove.
553
     */
554
    public function remove($key)
555
    {
556
        if (is_array($key)) {
2✔
557
            foreach ($key as $k) {
1✔
558
                unset($_SESSION[$k]);
1✔
559
            }
560

561
            return;
1✔
562
        }
563

564
        unset($_SESSION[$key]);
1✔
565
    }
566

567
    /**
568
     * Magic method to set variables in the session by simply calling
569
     *  $session->foo = bar;
570
     *
571
     * @param string       $key   Identifier of the session property to set.
572
     * @param array|string $value
573
     */
574
    public function __set(string $key, $value)
575
    {
576
        $_SESSION[$key] = $value;
1✔
577
    }
578

579
    /**
580
     * Magic method to get session variables by simply calling
581
     *  $foo = $session->foo;
582
     *
583
     * @param string $key Identifier of the session property to remove.
584
     *
585
     * @return string|null
586
     */
587
    public function __get(string $key)
588
    {
589
        // Note: Keep this order the same, just in case somebody wants to
590
        // use 'session_id' as a session data key, for whatever reason
591
        if (isset($_SESSION[$key])) {
1✔
592
            return $_SESSION[$key];
1✔
593
        }
594

595
        if ($key === 'session_id') {
×
596
            return session_id();
×
597
        }
598

599
        return null;
×
600
    }
601

602
    /**
603
     * Magic method to check for session variables.
604
     * Different from has() in that it will validate 'session_id' as well.
605
     * Mostly used by internal PHP functions, users should stick to has()
606
     *
607
     * @param string $key Identifier of the session property to remove.
608
     */
609
    public function __isset(string $key): bool
610
    {
611
        return isset($_SESSION[$key]) || ($key === 'session_id');
2✔
612
    }
613

614
    /**
615
     * Sets data into the session that will only last for a single request.
616
     * Perfect for use with single-use status update messages.
617
     *
618
     * If $data is an array, it is interpreted as an associative array of
619
     * key/value pairs for flashdata properties.
620
     * Otherwise, it is interpreted as the identifier of a specific
621
     * flashdata property, with $value containing the property value.
622
     *
623
     * @param array|string                            $data  Property identifier or associative array of properties
624
     * @param array|bool|float|int|object|string|null $value Property value if $data is a scalar
625
     */
626
    public function setFlashdata($data, $value = null)
627
    {
628
        $this->set($data, $value);
11✔
629
        $this->markAsFlashdata(is_array($data) ? array_keys($data) : $data);
11✔
630
    }
631

632
    /**
633
     * Retrieve one or more items of flash data from the session.
634
     *
635
     * If the item key is null, return all flashdata.
636
     *
637
     * @param string $key Property identifier
638
     *
639
     * @return array|null The requested property value, or an associative array  of them
640
     */
641
    public function getFlashdata(?string $key = null)
642
    {
643
        if (isset($key)) {
×
644
            return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key])
×
645
                && ! is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null;
×
646
        }
647

648
        $flashdata = [];
×
649

650
        if (isset($_SESSION['__ci_vars'])) {
×
651
            foreach ($_SESSION['__ci_vars'] as $key => &$value) {
×
652
                if (! is_int($value)) {
×
653
                    $flashdata[$key] = $_SESSION[$key];
×
654
                }
655
            }
656
        }
657

658
        return $flashdata;
×
659
    }
660

661
    /**
662
     * Keeps a single piece of flash data alive for one more request.
663
     *
664
     * @param array|string $key Property identifier or array of them
665
     */
666
    public function keepFlashdata($key)
667
    {
668
        $this->markAsFlashdata($key);
1✔
669
    }
670

671
    /**
672
     * Mark a session property or properties as flashdata.
673
     *
674
     * @param array|string $key Property identifier or array of them
675
     *
676
     * @return bool False if any of the properties are not already set
677
     */
678
    public function markAsFlashdata($key): bool
679
    {
680
        if (is_array($key)) {
11✔
681
            foreach ($key as $sessionKey) {
1✔
682
                if (! isset($_SESSION[$sessionKey])) {
1✔
683
                    return false;
×
684
                }
685
            }
686

687
            $new = array_fill_keys($key, 'new');
1✔
688

689
            $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) ? array_merge($_SESSION['__ci_vars'], $new) : $new;
1✔
690

691
            return true;
1✔
692
        }
693

694
        if (! isset($_SESSION[$key])) {
10✔
695
            return false;
×
696
        }
697

698
        $_SESSION['__ci_vars'][$key] = 'new';
10✔
699

700
        return true;
10✔
701
    }
702

703
    /**
704
     * Unmark data in the session as flashdata.
705
     *
706
     * @param array|string $key Property identifier or array of them
707
     */
708
    public function unmarkFlashdata($key)
709
    {
710
        if (! isset($_SESSION['__ci_vars'])) {
1✔
711
            return;
×
712
        }
713

714
        if (! is_array($key)) {
1✔
715
            $key = [$key];
1✔
716
        }
717

718
        foreach ($key as $k) {
1✔
719
            if (isset($_SESSION['__ci_vars'][$k]) && ! is_int($_SESSION['__ci_vars'][$k])) {
1✔
720
                unset($_SESSION['__ci_vars'][$k]);
1✔
721
            }
722
        }
723

724
        if ($_SESSION['__ci_vars'] === []) {
1✔
725
            unset($_SESSION['__ci_vars']);
1✔
726
        }
727
    }
728

729
    /**
730
     * Retrieve all of the keys for session data marked as flashdata.
731
     *
732
     * @return array The property names of all flashdata
733
     */
734
    public function getFlashKeys(): array
735
    {
736
        if (! isset($_SESSION['__ci_vars'])) {
2✔
737
            return [];
1✔
738
        }
739

740
        $keys = [];
1✔
741

742
        foreach (array_keys($_SESSION['__ci_vars']) as $key) {
1✔
743
            if (! is_int($_SESSION['__ci_vars'][$key])) {
1✔
744
                $keys[] = $key;
1✔
745
            }
746
        }
747

748
        return $keys;
1✔
749
    }
750

751
    /**
752
     * Sets new data into the session, and marks it as temporary data
753
     * with a set lifespan.
754
     *
755
     * @param array|string                            $data  Session data key or associative array of items
756
     * @param array|bool|float|int|object|string|null $value Value to store
757
     * @param int                                     $ttl   Time-to-live in seconds
758
     */
759
    public function setTempdata($data, $value = null, int $ttl = 300)
760
    {
761
        $this->set($data, $value);
9✔
762
        $this->markAsTempdata($data, $ttl);
9✔
763
    }
764

765
    /**
766
     * Returns either a single piece of tempdata, or all temp data currently
767
     * in the session.
768
     *
769
     * @param string $key Session data key
770
     *
771
     * @return array|bool|float|int|object|string|null Session data value or null if not found.
772
     */
773
    public function getTempdata(?string $key = null)
774
    {
775
        if (isset($key)) {
5✔
776
            return (isset($_SESSION['__ci_vars'], $_SESSION['__ci_vars'][$key], $_SESSION[$key])
1✔
777
                    && is_int($_SESSION['__ci_vars'][$key])) ? $_SESSION[$key] : null;
1✔
778
        }
779

780
        $tempdata = [];
4✔
781

782
        if (isset($_SESSION['__ci_vars'])) {
4✔
783
            foreach ($_SESSION['__ci_vars'] as $key => &$value) {
3✔
784
                if (is_int($value)) {
3✔
785
                    $tempdata[$key] = $_SESSION[$key];
3✔
786
                }
787
            }
788
        }
789

790
        return $tempdata;
4✔
791
    }
792

793
    /**
794
     * Removes a single piece of temporary data from the session.
795
     *
796
     * @param string $key Session data key
797
     */
798
    public function removeTempdata(string $key)
799
    {
800
        $this->unmarkTempdata($key);
1✔
801
        unset($_SESSION[$key]);
1✔
802
    }
803

804
    /**
805
     * Mark one of more pieces of data as being temporary, meaning that
806
     * it has a set lifespan within the session.
807
     *
808
     * @param array|string $key Property identifier or array of them
809
     * @param int          $ttl Time to live, in seconds
810
     *
811
     * @return bool False if any of the properties were not set
812
     */
813
    public function markAsTempdata($key, int $ttl = 300): bool
814
    {
815
        $ttl += Time::now()->getTimestamp();
9✔
816

817
        if (is_array($key)) {
9✔
818
            $temp = [];
8✔
819

820
            foreach ($key as $k => $v) {
8✔
821
                // Do we have a key => ttl pair, or just a key?
822
                if (is_int($k)) {
8✔
823
                    $k = $v;
1✔
824
                    $v = $ttl;
1✔
825
                } elseif (is_string($v)) {
7✔
826
                    $v = Time::now()->getTimestamp() + $ttl;
6✔
827
                } else {
828
                    $v += Time::now()->getTimestamp();
1✔
829
                }
830

831
                if (! array_key_exists($k, $_SESSION)) {
8✔
832
                    return false;
×
833
                }
834

835
                $temp[$k] = $v;
8✔
836
            }
837

838
            $_SESSION['__ci_vars'] = isset($_SESSION['__ci_vars']) ? array_merge($_SESSION['__ci_vars'], $temp) : $temp;
8✔
839

840
            return true;
8✔
841
        }
842

843
        if (! isset($_SESSION[$key])) {
1✔
844
            return false;
×
845
        }
846

847
        $_SESSION['__ci_vars'][$key] = $ttl;
1✔
848

849
        return true;
1✔
850
    }
851

852
    /**
853
     * Unmarks temporary data in the session, effectively removing its
854
     * lifespan and allowing it to live as long as the session does.
855
     *
856
     * @param array|string $key Property identifier or array of them
857
     */
858
    public function unmarkTempdata($key)
859
    {
860
        if (! isset($_SESSION['__ci_vars'])) {
3✔
861
            return;
×
862
        }
863

864
        if (! is_array($key)) {
3✔
865
            $key = [$key];
2✔
866
        }
867

868
        foreach ($key as $k) {
3✔
869
            if (isset($_SESSION['__ci_vars'][$k]) && is_int($_SESSION['__ci_vars'][$k])) {
3✔
870
                unset($_SESSION['__ci_vars'][$k]);
3✔
871
            }
872
        }
873

874
        if ($_SESSION['__ci_vars'] === []) {
3✔
875
            unset($_SESSION['__ci_vars']);
1✔
876
        }
877
    }
878

879
    /**
880
     * Retrieve the keys of all session data that have been marked as temporary data.
881
     */
882
    public function getTempKeys(): array
883
    {
884
        if (! isset($_SESSION['__ci_vars'])) {
2✔
885
            return [];
1✔
886
        }
887

888
        $keys = [];
1✔
889

890
        foreach (array_keys($_SESSION['__ci_vars']) as $key) {
1✔
891
            if (is_int($_SESSION['__ci_vars'][$key])) {
1✔
892
                $keys[] = $key;
1✔
893
            }
894
        }
895

896
        return $keys;
1✔
897
    }
898

899
    /**
900
     * Sets the driver as the session handler in PHP.
901
     * Extracted for easier testing.
902
     */
903
    protected function setSaveHandler()
904
    {
905
        session_set_save_handler($this->driver, true);
39✔
906
    }
907

908
    /**
909
     * Starts the session.
910
     * Extracted for testing reasons.
911
     */
912
    protected function startSession()
913
    {
914
        if (ENVIRONMENT === 'testing') {
39✔
915
            $_SESSION = [];
39✔
916

917
            return;
39✔
918
        }
919

UNCOV
920
        session_start(); // @codeCoverageIgnore
×
921
    }
922

923
    /**
924
     * Takes care of setting the cookie on the client side.
925
     *
926
     * @codeCoverageIgnore
927
     */
928
    protected function setCookie()
929
    {
UNCOV
930
        $expiration   = $this->config->expiration === 0 ? 0 : Time::now()->getTimestamp() + $this->config->expiration;
×
UNCOV
931
        $this->cookie = $this->cookie->withValue(session_id())->withExpires($expiration);
×
932

UNCOV
933
        $response = service('response');
×
UNCOV
934
        $response->setCookie($this->cookie);
×
935
    }
936
}
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