Skip to content

Commit

Permalink
Merge pull request #80 from aleksip/fix-early-return
Browse files Browse the repository at this point in the history
Fix warning in SessionManager::destroy() for headers already sent and actual fix for trying to destroy non-existent session
  • Loading branch information
Xerkus authored Feb 29, 2024
2 parents 42f30f2 + ca80cb4 commit cbcd9f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ protected function initializeValidatorChain()
*/
public function destroy(?array $options = null)
{
if (headers_sent() || ! $this->sessionExists()) {
// session_destroy() requires active session while method
// $this->sessionExists() includes other conditions
if (session_status() !== PHP_SESSION_ACTIVE) {
return;
}

Expand All @@ -205,7 +207,7 @@ public function destroy(?array $options = null)
}

session_destroy();
if ($options['send_expire_cookie']) {
if (! headers_sent() && $options['send_expire_cookie']) {
$this->expireSessionCookie();
}

Expand Down
16 changes: 16 additions & 0 deletions test/SessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function extension_loaded;
use function headers_sent;
use function ini_get;
use function ob_flush;
use function preg_match;
use function range;
use function restore_error_handler;
Expand Down Expand Up @@ -481,6 +482,21 @@ public function testPassingClearStorageOptionWhenCallingDestroyClearsStorage():
self::assertFalse(isset($storage['foo']));
}

/**
* @runInSeparateProcess
*/
public function testDestroySessionWhenHeadersHaveBeenSent(): void
{
$this->manager = new SessionManager();
$this->manager->start();
$storage = $this->manager->getStorage();
$storage['foo'] = 'bar';
echo ' ';
ob_flush();
$this->manager->destroy(['clear_storage' => true]);
self::assertFalse(isset($storage['foo']));
}

/**
* @runInSeparateProcess
*/
Expand Down

0 comments on commit cbcd9f7

Please sign in to comment.