Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit b8e99db

Browse files
committed
🔨 improve session feature
Signed-off-by: otengkwame <[email protected]>
1 parent 99a59ba commit b8e99db

10 files changed

+229
-93
lines changed

framework/libraries/Session/CI_Session_driver_interface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @package CodeIgniter
3131
* @author EllisLab Dev Team
3232
* @copyright Copyright (c) 2022, CodeIgniter Foundation (https://codeigniter.com/)
33-
* @license http://opensource.org/licenses/MIT MIT License
33+
* @license https://opensource.org/licenses/MIT MIT License
3434
* @link https://codeigniter.com
3535
* @since Version 3.0.0
3636
* @filesource
@@ -57,4 +57,6 @@ public function read($session_id);
5757
public function write($session_id, $session_data);
5858
public function destroy($session_id);
5959
public function gc($maxlifetime);
60+
public function updateTimestamp($session_id, $data);
61+
public function validateId($session_id);
6062
}

framework/libraries/Session/OldSessionWrapper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @package CodeIgniter
3131
* @author EllisLab Dev Team
3232
* @copyright Copyright (c) 2022, CodeIgniter Foundation (https://codeigniter.com/)
33-
* @license http://opensource.org/licenses/MIT MIT License
33+
* @license https://opensource.org/licenses/MIT MIT License
3434
* @link https://codeigniter.com
3535
* @since Version 3.0.0
3636
* @filesource
@@ -49,7 +49,7 @@
4949
* @author Developer Kwame
5050
* @link https://codeigniter.com/userguide3/libraries/sessions.html
5151
*/
52-
class CI_SessionWrapper implements SessionHandlerInterface
52+
class CI_SessionWrapper implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface
5353
{
5454

5555
protected $driver;
@@ -90,4 +90,15 @@ public function gc($maxlifetime): bool
9090
{
9191
return $this->driver->gc($maxlifetime);
9292
}
93+
94+
public function updateTimestamp($id, $data): bool
95+
{
96+
return $this->driver->updateTimestamp($id, $data);
97+
}
98+
99+
public function validateId($id): bool
100+
{
101+
return $this->driver->validateId($id);
102+
}
103+
93104
}

framework/libraries/Session/PHP8SessionWrapper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @package CodeIgniter
3131
* @author EllisLab Dev Team
3232
* @copyright Copyright (c) 2022, CodeIgniter Foundation (https://codeigniter.com/)
33-
* @license http://opensource.org/licenses/MIT MIT License
33+
* @license https://opensource.org/licenses/MIT MIT License
3434
* @link https://codeigniter.com
3535
* @since Version 3.0.0
3636
* @filesource
@@ -48,7 +48,7 @@
4848
* @author Andrey Andreev
4949
* @link https://codeigniter.com/userguide3/libraries/sessions.html
5050
*/
51-
class CI_PHP8SessionWrapper implements SessionHandlerInterface
51+
class CI_PHP8SessionWrapper implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface
5252
{
5353

5454
protected CI_Session_driver_interface $driver;
@@ -89,4 +89,15 @@ public function gc(int $maxlifetime): mixed
8989
{
9090
return $this->driver->gc($maxlifetime);
9191
}
92+
93+
public function updateTimestamp(string $id, string $data): bool
94+
{
95+
return $this->driver->updateTimestamp($id, $data);
96+
}
97+
98+
public function validateId(string $id): bool
99+
{
100+
return $this->driver->validateId($id);
101+
}
102+
92103
}

framework/libraries/Session/Session.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ public function __construct(array $params = [])
192192
*/
193193
protected function _ci_load_classes($driver)
194194
{
195+
// PHP 7 compatibility
196+
interface_exists('SessionUpdateTimestampHandlerInterface', false) or require_once(BASEPATH . 'libraries/Session/SessionUpdateTimestampHandlerInterface.php');
197+
195198
require_once(BASEPATH . 'libraries/Session/CI_Session_driver_interface.php');
196199
$wrapper = is_php('8.0') ? 'PHP8SessionWrapper' : 'OldSessionWrapper';
197200
require_once(BASEPATH . 'libraries/Session/' . $wrapper . '.php');
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* CodeIgniter
4+
*
5+
* An open source application development framework for PHP
6+
*
7+
* This content is released under the MIT License (MIT)
8+
*
9+
* Copyright (c) 2019 - 2022, CodeIgniter Foundation
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
* THE SOFTWARE.
28+
*
29+
* @package CodeIgniter
30+
* @author EllisLab Dev Team
31+
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32+
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33+
* @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
34+
* @license https://opensource.org/licenses/MIT MIT License
35+
* @link https://codeigniter.com
36+
* @since Version 3.0.0
37+
* @filesource
38+
*/
39+
defined('BASEPATH') OR exit('No direct script access allowed');
40+
41+
/**
42+
* SessionUpdateTimestampHandlerInterface
43+
*
44+
* PHP 7 compatibility interface
45+
*
46+
* @package CodeIgniter
47+
* @subpackage Libraries
48+
* @category Sessions
49+
* @author Andrey Andreev
50+
* @link https://codeigniter.com/userguide3/libraries/sessions.html
51+
*/
52+
interface SessionUpdateTimestampHandlerInterface {
53+
54+
public function updateTimestamp($session_id, $data);
55+
public function validateId($session_id);
56+
}

framework/libraries/Session/Session_driver.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ public function __construct(&$params)
112112

113113
// ------------------------------------------------------------------------
114114

115+
/**
116+
* PHP 5.x validate ID
117+
*
118+
* Enforces session.use_strict_mode
119+
*
120+
* @return void
121+
*/
122+
public function php5_validate_id()
123+
{
124+
if ($this->_success === 0 && isset($_COOKIE[$this->_config['cookie_name']]) && !$this->validateId($_COOKIE[$this->_config['cookie_name']])) {
125+
unset($_COOKIE[$this->_config['cookie_name']]);
126+
}
127+
}
128+
129+
// ------------------------------------------------------------------------
130+
115131
/**
116132
* Cookie destroy
117133
*

0 commit comments

Comments
 (0)