From 42909c20c787a6486bad97ac6995e13963b70eb8 Mon Sep 17 00:00:00 2001 From: Chris Weller Date: Tue, 19 Mar 2019 11:39:48 +0000 Subject: [PATCH 1/3] Update SessionHandler.php If there is no session on reading the result was returned as null, it now returns an empty string which PHP handles without error. --- src/SessionHandler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SessionHandler.php b/src/SessionHandler.php index 9dce4de..9988c63 100644 --- a/src/SessionHandler.php +++ b/src/SessionHandler.php @@ -69,6 +69,7 @@ public function read($id) { $row = $result->fetch_assoc(); $result = $row['data']; + $result = $result === null ? "" : $result; } return $result; @@ -101,4 +102,4 @@ public function gc($maxlifetime) return $this->dbConnection->query($sql); } -} \ No newline at end of file +} From f9c0ce48e32669592526b773247745212e54f708 Mon Sep 17 00:00:00 2001 From: Chris Weller <1003556+ChrisWeller@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:00:20 +0100 Subject: [PATCH 2/3] Update to support PHP 8 with type definitions --- composer.json | 2 +- src/SessionHandler.php | 132 ++++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/composer.json b/composer.json index 32b1bb6..b131334 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": ">=7.0.0" + "php": ">=8.0.0" }, "autoload": { "psr-4": { diff --git a/src/SessionHandler.php b/src/SessionHandler.php index 3c63637..b9a9f38 100644 --- a/src/SessionHandler.php +++ b/src/SessionHandler.php @@ -5,25 +5,25 @@ final class SessionHandler implements \SessionHandlerInterface { - private $dbConnection; # the mysqli connection - private $dbTable; # name of the db table to store sessions in - private $m_maxAge; - - - /** - * Create the session handler. - * @param \mysqli $mysqli - the database connection to store sessions in. - * @param string $tableName - the table within the database to store session data in. - * @param int $maxAge - the maximum age in seconds of a session variable. - */ - public function __construct(\mysqli $mysqli, string $tableName, int $maxAge=86400) - { - $this->dbConnection = $mysqli; - $this->dbTable = $tableName; - $this->m_maxAge = $maxAge; - - $createSessionsTableQuery = - "CREATE TABLE IF NOT EXISTS `{$this->dbTable}` ( + private $dbConnection; # the mysqli connection + private $dbTable; # name of the db table to store sessions in + private $m_maxAge; + + + /** + * Create the session handler. + * @param \mysqli $mysqli - the database connection to store sessions in. + * @param string $tableName - the table within the database to store session data in. + * @param int $maxAge - the maximum age in seconds of a session variable. + */ + public function __construct(\mysqli $mysqli, string $tableName, int $maxAge=86400) + { + $this->dbConnection = $mysqli; + $this->dbTable = $tableName; + $this->m_maxAge = $maxAge; + + $createSessionsTableQuery = + "CREATE TABLE IF NOT EXISTS `{$this->dbTable}` ( `id` varchar(32) NOT NULL, `modified_timestamp` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `data` mediumtext, @@ -31,66 +31,66 @@ public function __construct(\mysqli $mysqli, string $tableName, int $maxAge=8640 KEY `modified_timestamp` (`modified_timestamp`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; - $this->dbConnection->query($createSessionsTableQuery); - } + $this->dbConnection->query($createSessionsTableQuery); + } - public function open($savePath, $sessionName) - { - $sql = "DELETE FROM `{$this->dbTable}` WHERE `modified_timestamp` < (NOW() - INTERVAL {$this->m_maxAge} SECOND)"; - return $this->dbConnection->query($sql); - } + public function open(string $savePath, string $sessionName) : bool + { + $sql = "DELETE FROM `{$this->dbTable}` WHERE `modified_timestamp` < (NOW() - INTERVAL {$this->m_maxAge} SECOND)"; + return $this->dbConnection->query($sql); + } - public function close() - { - return $this->dbConnection->close(); - } + public function close():bool + { + return $this->dbConnection->close(); + } - public function read($id) - { - $sql = "SELECT `data` FROM `{$this->dbTable}` WHERE `id` = '{$id}'"; - $result = $this->dbConnection->query($sql); + public function read($id):string|false + { + $sql = "SELECT `data` FROM `{$this->dbTable}` WHERE `id` = '{$id}'"; + $result = $this->dbConnection->query($sql); - if ($result === false) - { - throw new \Exception("There is an issue with your session handler using MySQL."); - } + if ($result === false) + { + throw new \Exception("There is an issue with your session handler using MySQL."); + } - if ($result->num_rows === 0) - { - $result = ""; - } - else - { - $row = $result->fetch_assoc(); - $result = $row['data']; - $result = $result === null ? "" : $result; - } + if ($result->num_rows === 0) + { + $result = ""; + } + else + { + $row = $result->fetch_assoc(); + $result = $row['data']; + $result = $result === null ? "" : $result; + } - return $result; - } + return $result; + } - public function write($id, $data) - { - $sql = "REPLACE INTO `{$this->dbTable}` (id, data) VALUES('{$id}', '{$data}')"; - return $this->dbConnection->query($sql); - } + public function write(string $id, string $data):bool + { + $sql = "REPLACE INTO `{$this->dbTable}` (id, data) VALUES('{$id}', '{$data}')"; + return $this->dbConnection->query($sql); + } - public function destroy($id) - { - $sql = "DELETE FROM `{$this->dbTable}` WHERE `id` = '{$id}'"; - return $this->dbConnection->query($sql); - } + public function destroy(string $id):bool + { + $sql = "DELETE FROM `{$this->dbTable}` WHERE `id` = '{$id}'"; + return $this->dbConnection->query($sql); + } - public function gc($maxlifetime) - { - $minTime = time() - intval($maxlifetime); - $sql = "DELETE FROM `{$this->dbTable}` WHERE `timestamp` < '{$minTime}'"; - return $this->dbConnection->query($sql); - } + public function gc(int $maxlifetime):int|false + { + $minTime = time() - intval($maxlifetime); + $sql = "DELETE FROM `{$this->dbTable}` WHERE `timestamp` < '{$minTime}'"; + return $this->dbConnection->query($sql); + } } From d54c17a4bae681e1ee3de0b57f0bedcbf966b641 Mon Sep 17 00:00:00 2001 From: Chris Weller <1003556+ChrisWeller@users.noreply.github.com> Date: Thu, 31 Aug 2023 10:05:30 +0100 Subject: [PATCH 3/3] Garbage collection updated to use modified_timestamp field instead of timestamp field --- .gitignore | 1 + src/SessionHandler.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 88afc05..a1eadcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock test/ vendor/ +/.idea/ diff --git a/src/SessionHandler.php b/src/SessionHandler.php index b9a9f38..127c107 100644 --- a/src/SessionHandler.php +++ b/src/SessionHandler.php @@ -90,7 +90,7 @@ public function destroy(string $id):bool public function gc(int $maxlifetime):int|false { $minTime = time() - intval($maxlifetime); - $sql = "DELETE FROM `{$this->dbTable}` WHERE `timestamp` < '{$minTime}'"; + $sql = "DELETE FROM `{$this->dbTable}` WHERE `modified_timestamp` < '{$minTime}'"; return $this->dbConnection->query($sql); } }