From 00a3ca2fb14717ce86d52323936e32971805f78a Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Thu, 5 Dec 2024 19:50:49 -0300 Subject: [PATCH 1/2] Add compatibility with PHP 8.4 --- src/SaveHandler.php | 4 ++++ src/Session.php | 6 ++++-- tests/SaveHandlers/AbstractHandler.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/SaveHandler.php b/src/SaveHandler.php index 5a0d2b6..fa25b5a 100644 --- a/src/SaveHandler.php +++ b/src/SaveHandler.php @@ -195,11 +195,15 @@ protected function getKeySuffix() : string * @param string $id The session id * * @see https://www.php.net/manual/en/sessionupdatetimestamphandlerinterface.validateid.php + * @see https://wiki.php.net/rfc/deprecations_php_8_4#sessionsid_length_and_sessionsid_bits_per_character * * @return bool Returns TRUE if the id is valid, otherwise FALSE */ public function validateId($id) : bool { + if (\PHP_VERSION_ID >= 80400) { + return (bool) \preg_match('#\A[0-9a-f]{32}\z#', $id); + } $bits = \ini_get('session.sid_bits_per_character') ?: 5; $length = \ini_get('session.sid_length') ?: 40; $bitsRegex = [ diff --git a/src/Session.php b/src/Session.php index 7b34489..291fb97 100644 --- a/src/Session.php +++ b/src/Session.php @@ -82,8 +82,6 @@ protected function setOptions(array $custom) : void $default = [ 'name' => 'session_id', 'serialize_handler' => $serializer, - 'sid_bits_per_character' => 6, - 'sid_length' => 48, 'cookie_domain' => '', 'cookie_httponly' => 1, 'cookie_lifetime' => 7200, @@ -99,6 +97,10 @@ protected function setOptions(array $custom) : void 'auto_regenerate_maxlifetime' => 0, 'auto_regenerate_destroy' => true, ]; + if (\PHP_VERSION_ID < 80400) { + $default['sid_bits_per_character'] = 6; + $default['sid_length'] = 48; + } $this->options = $custom ? \array_replace($default, $custom) : $default; diff --git a/tests/SaveHandlers/AbstractHandler.php b/tests/SaveHandlers/AbstractHandler.php index 98680ad..58ee232 100644 --- a/tests/SaveHandlers/AbstractHandler.php +++ b/tests/SaveHandlers/AbstractHandler.php @@ -44,12 +44,31 @@ protected function replaceConfig(array $config) : void $this->config = \array_replace_recursive($this->config, $config); } + /** + * @see https://wiki.php.net/rfc/deprecations_php_8_4#sessionsid_length_and_sessionsid_bits_per_character + */ public function testValidateId() : void { + // Ids with PHP lower than 8.4: $id6 = '62my7tSXcbIrOZ-WHsEXhpwUoG,afmBQNGaSBkFN'; $id5 = 'iimuf8lvdectatt5jtkve15831funl8rg5cg6okp'; $id4 = '96aa2c863140e0e714a603cf44b0afc9a0632592'; $this->session->stop(); + /* + * Since PHP 8.4 session.sid_bits_per_character and session.sid_length + * are deprecated. + * + * - The default value for session.sid_bits_per_character is 4 (0-9, a-f). + * - The default value for session.sid_length is 32 character. + */ + if (\PHP_VERSION_ID >= 80400) { + self::assertFalse($this->handler->validateId($id6)); + self::assertFalse($this->handler->validateId($id5)); + self::assertFalse($this->handler->validateId($id4)); + $idPhp84 = '96aa2c863140e0e714a603cf44b0afc9'; + self::assertTrue($this->handler->validateId($idPhp84)); + return; + } \ini_set('session.sid_bits_per_character', '6'); \ini_set('session.sid_length', '40'); self::assertTrue($this->handler->validateId($id6)); From 44765b0ec74a678ea59606ea7f7878d8d0830e69 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Thu, 5 Dec 2024 20:00:15 -0300 Subject: [PATCH 2/2] Add note --- src/SaveHandlers/DatabaseHandler.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/SaveHandlers/DatabaseHandler.php b/src/SaveHandlers/DatabaseHandler.php index 46ebdc0..cd66006 100644 --- a/src/SaveHandlers/DatabaseHandler.php +++ b/src/SaveHandlers/DatabaseHandler.php @@ -35,6 +35,8 @@ * ); * ``` * + * NOTE: As of PHP 8.4 the id column can be `char(32)`. + * * @package session */ class DatabaseHandler extends SaveHandler