Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #24 from Federkun/use-lifetime-from-php-setting
Browse files Browse the repository at this point in the history
Use `session.cookie_lifetime` value when creating the session cookie.
  • Loading branch information
weierophinney committed Sep 12, 2018
2 parents 8aa9be0 + 6ce9805 commit f7617be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/PhpSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
->withValue($this->cookie)
->withPath(ini_get('session.cookie_path'));

if ($cookieLifetime = (int) ini_get('session.cookie_lifetime')) {
$sessionCookie = $sessionCookie->withExpires(time() + $cookieLifetime);
}

$response = FigResponseCookies::set($response, $sessionCookie);

if (! $this->cacheLimiter || $this->responseAlreadyHasCacheHeaders($response)) {
Expand Down
41 changes: 41 additions & 0 deletions test/PhpSessionPersistenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,45 @@ public function testCookiesSetWithoutRegenerate()

$this->assertNotEmpty($response->getHeaderLine('Set-Cookie'));
}

public function testCookiesSetWithDefaultLifetime()
{
$persistence = new PhpSessionPersistence();
$request = new ServerRequest();
$session = $persistence->initializeSessionFromRequest($request);

$session->set('foo', 'bar');

$response = $persistence->persistSession($session, new Response());

$setCookie = FigResponseCookies::get($response, session_name());

$this->assertNotEmpty($response->getHeaderLine('Set-Cookie'));
$this->assertInstanceOf(SetCookie::class, $setCookie);
$this->assertSame(0, $setCookie->getExpires());
}

public function testCookiesSetWithCustomLifetime()
{
$lifetime = 300;
$expectedTimestamp = time() + $lifetime;

$ini = $this->applyCustomSessionOptions([
'cookie_lifetime' => $lifetime,
]);

$persistence = new PhpSessionPersistence();
$request = new ServerRequest();
$session = $persistence->initializeSessionFromRequest($request);

$session->set('foo', 'bar');

$response = $persistence->persistSession($session, new Response());

$setCookie = FigResponseCookies::get($response, session_name());
$this->assertInstanceOf(SetCookie::class, $setCookie);
$this->assertSame($expectedTimestamp, $setCookie->getExpires());

$this->restoreOriginalSessionIniSettings($ini);
}
}

0 comments on commit f7617be

Please sign in to comment.