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/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 1aa3160..127c107 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,65 +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']; - } + 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); - } -} \ No newline at end of file + public function gc(int $maxlifetime):int|false + { + $minTime = time() - intval($maxlifetime); + $sql = "DELETE FROM `{$this->dbTable}` WHERE `modified_timestamp` < '{$minTime}'"; + return $this->dbConnection->query($sql); + } +}