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

destroy session first on regenerate session when session is active #7

Merged
Merged
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
8 changes: 5 additions & 3 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use function gmdate;
use function ini_get;
use function random_bytes;
use function session_destroy;
use function session_id;
use function session_name;
use function session_start;
Expand Down Expand Up @@ -186,12 +187,13 @@ private function startSession(string $id, array $options = []) : void

/**
* Regenerates the session safely.
*
* @link http://php.net/manual/en/function.session-regenerate-id.php (Example #2)
*/
private function regenerateSession() : string
{
session_write_close();
if (PHP_SESSION_ACTIVE === session_status()) {
session_destroy();
}

$id = $this->generateSessionId();
$this->startSession($id, [
'use_strict_mode' => false,
Expand Down
19 changes: 19 additions & 0 deletions test/PhpSessionPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
use function ini_get;
use function session_id;
use function session_name;
use function session_save_path;
use function session_start;
use function session_status;
use function sys_get_temp_dir;
use function time;

use const PHP_SESSION_ACTIVE;
Expand Down Expand Up @@ -910,6 +912,23 @@ public function testInitializeIdRegeneratesSessionId()
$this->assertFalse($actual->isRegenerated());
}

public function testRegenerateWhenSessionAlreadyActiveDestroyExistingSessionFirst()
{
session_start();

$_SESSION['test'] = 'value';
$fileSession = (session_save_path() ?: sys_get_temp_dir()) . '/sess_' . session_id();

$this->assertFileExists($fileSession);

$persistence = new PhpSessionPersistence();
$session = new Session(['foo' => 'bar']);
$session = $session->regenerate();
$persistence->persistSession($session, new Response());

$this->assertFileNotExists($fileSession);
}

public function testInitializeIdReturnsSessionUnaltered()
{
$persistence = new PhpSessionPersistence();
Expand Down