Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Enh #86: Remove `yiisoft/cookies` dependency (@vjik)
- Bug #86: `NullSession::getCookieParameters()` now returns proper cookie parameters instead of an empty array (@vjik)
- Bug #25: Fix reusing the previous session ID in `Session::open()` in long-running workers (@klsoft-web)

## 3.0.2 August 26, 2026

Expand Down
11 changes: 7 additions & 4 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Session;

use SessionHandlerInterface;
use SessionHandler;
use Throwable;

use const PHP_SESSION_ACTIVE;
Expand Down Expand Up @@ -37,14 +38,14 @@

/**
* @param array $options Session options. See {@link https://www.php.net/manual/en/session.configuration.php}.
* @param SessionHandlerInterface|null $handler Session handler. If not specified, default PHP handler is used.
* @param SessionHandlerInterface|SessionHandler|null $handler Session handler. If not specified, default PHP handler is used.
*
* @psalm-param SessionOptions $options
*/
public function __construct(array $options = [], ?SessionHandlerInterface $handler = null)
public function __construct(array $options = [], private SessionHandlerInterface|SessionHandler|null $handler = null)
{
if ($handler !== null) {
session_set_save_handler($handler, true);
if ($this->handler !== null) {
session_set_save_handler($this->handler, true);
}

// We set cookies using SessionMiddleware.
Expand Down Expand Up @@ -94,6 +95,8 @@

if ($this->sessionId !== null) {
session_id($this->sessionId);
} else if ($this->handler instanceof SessionHandler) {
session_id($this->handler->create_sid());

Check failure on line 99 in src/Session.php

View workflow job for this annotation

GitHub Actions / psalm80 / PHP 8.0-ubuntu-latest

InvalidArgument

src/Session.php:99:24: InvalidArgument: Argument 1 of session_id expects string, but char provided (see https://psalm.dev/004)
}

try {
Expand Down
17 changes: 17 additions & 0 deletions tests/MockSessionId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Yiisoft\Session\Tests;

use SessionHandler;

final class MockSessionId extends SessionHandler
{
public function __construct(private string $sessionId)
{
}

public function create_sid(): string
{
return $this->sessionId;
}
}
13 changes: 13 additions & 0 deletions tests/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public function testRegenerateIdOpensInactiveSession(): void
self::assertNotEquals($id, $session->getId());
}

public function testOpenDoesNotReuseIdOfClosedSession(): void
{
$firstSession = new Session([], new MockSessionId('first-session-id'));
$firstSession->open();
$firstId = $firstSession->getId();
$firstSession->close();

$secondSession = new Session([], new MockSessionId('second-session-id'));
$secondSession->open();

self::assertNotSame($firstId, $secondSession->getId());
}

public function testDiscard(): void
{
$session = $this->getSession();
Expand Down
Loading