From f6b3330a930db18025d21334c55ca96bd5cf3fed Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 25 Aug 2026 20:00:05 +0300 Subject: [PATCH 1/2] Open session in `Session::regenerateId()` if it is not active yet --- CHANGELOG.md | 3 ++- src/Session.php | 20 ++++++++++---------- src/SessionInterface.php | 1 + tests/SessionTest.php | 11 +++++++++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f04b5bf..bec6c63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 3.0.2 under development -- Chg #81: Add explicit use imports for classes and constants (@vjik) +- Enh #81: Add explicit use imports for classes and constants (@vjik) +- Bug #85: Open session in `Session::regenerateId()` if it is not active yet (@vjik) ## 3.0.1 December 17, 2025 diff --git a/src/Session.php b/src/Session.php index a83fede..24150da 100644 --- a/src/Session.php +++ b/src/Session.php @@ -117,17 +117,17 @@ public function getId(): ?string public function regenerateId(): void { - if ($this->isActive()) { - try { - if (session_regenerate_id(true)) { - /** - * @var string Without `id` parameter `session_id()` always returns string. - */ - $this->sessionId = session_id(); - } - } catch (Throwable $e) { - throw new SessionException('Failed to regenerate ID.', (int) $e->getCode(), $e); + $this->open(); + + try { + if (session_regenerate_id(true)) { + /** + * @var string Without `id` parameter `session_id()` always returns string. + */ + $this->sessionId = session_id(); } + } catch (Throwable $e) { + throw new SessionException('Failed to regenerate ID.', (int) $e->getCode(), $e); } } diff --git a/src/SessionInterface.php b/src/SessionInterface.php index 5e44dd2..563285c 100644 --- a/src/SessionInterface.php +++ b/src/SessionInterface.php @@ -54,6 +54,7 @@ public function setId(string $sessionId): void; /** * Regenerate session ID keeping data. + * Opens the session first if it is not active yet. */ public function regenerateId(): void; diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 3d38dbd..707af8c 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -82,6 +82,17 @@ public function testRegenerateID(): void self::assertNotEquals($id, $session->getId()); } + public function testRegenerateIdOpensInactiveSession(): void + { + $session = $this->getSession(); + self::assertFalse($session->isActive()); + + $session->regenerateId(); + + self::assertTrue($session->isActive()); + self::assertNotNull($session->getId()); + } + public function testDiscard(): void { $session = $this->getSession(); From 1458736ce74f2f649a3e6b270d42e85fb5d4ee54 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 25 Aug 2026 20:06:21 +0300 Subject: [PATCH 2/2] fix test --- tests/SessionTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/SessionTest.php b/tests/SessionTest.php index 707af8c..97efff2 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -85,12 +85,15 @@ public function testRegenerateID(): void public function testRegenerateIdOpensInactiveSession(): void { $session = $this->getSession(); + $session->open(); + $id = $session->getId(); + $session->close(); self::assertFalse($session->isActive()); $session->regenerateId(); self::assertTrue($session->isActive()); - self::assertNotNull($session->getId()); + self::assertNotEquals($id, $session->getId()); } public function testDiscard(): void