Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove id check when checking for started session #98

Open
wants to merge 6 commits into
base: 2.23.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use function array_key_exists;
use function array_merge;
use function constant;
use function defined;
use function headers_sent;
use function is_array;
use function iterator_to_array;
Expand Down Expand Up @@ -91,12 +93,19 @@ public function sessionExists()
return true;
}

if ($this->getId()) {
/**
* @var string|false $sid
*/
$sid = defined('SID') ? constant('SID') : false;

if ($sid !== false && $this->getId()) {
return true;
}

if (headers_sent()) {
return true;
}

return false;
}

Expand Down
25 changes: 25 additions & 0 deletions test/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laminas\Session\Validator\Id;
use Laminas\Session\Validator\RemoteAddr;
use LaminasTest\Session\TestAsset\Php81CompatibleStorageInterface;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use Traversable;

Expand All @@ -35,6 +36,7 @@
use function session_write_close;
use function set_error_handler;
use function stristr;
use function uniqid;
use function var_export;
use function xdebug_get_headers;

Expand Down Expand Up @@ -902,6 +904,29 @@ public function testIdValidationWillFailOnInvalidData(): void
$this->manager->start();
}

#[RunInSeparateProcess]
mimmi20 marked this conversation as resolved.
Show resolved Hide resolved
public function testSettingTheIdentifierBeforeStartingTheSessionYieldsTheExpectedId(): void
{
$manager = new SessionManager();

$id = uniqid();

$manager->setId($id);

// setting a session id does not mark a session as started
self::assertFalse($manager->sessionExists());

$manager->start();

self::assertTrue($manager->sessionExists());

$manager->writeClose();

// calling writeClose() does not mark the session as closed
self::assertTrue($manager->sessionExists());
self::assertSame($id, $manager->getId());
}

/** @param non-empty-string $property */
private function assertAttributeEquals(mixed $expected, string $property, object $object): void
{
Expand Down
Loading