8000 [HttpFoundation] Fixed session migration with custom cookie lifetime · symfony/symfony@3e824de · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e824de

Browse files
Guitenicolas-grekas
authored andcommitted
[HttpFoundation] Fixed session migration with custom cookie lifetime
1 parent 3ebe15e commit 3e824de

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function start()
152152

153153
// ok to try and start the session
154154
if (!session_start()) {
155-
throw new \RuntimeException('Failed to start the session');
155+
throw new \RuntimeException('Failed to start the session.');
156156
}
157157

158158
if (null !== $this->emulateSameSite) {
@@ -213,8 +213,10 @@ public function regenerate($destroy = false, $lifetime = null)
213213
return false;
214214
}
215215

216-
if (null !== $lifetime) {
216+
if (null !== $lifetime && $lifetime != ini_get('session.cookie_lifetime')) {
217+
$this->save();
217218
ini_set('session.cookie_lifetime', $lifetime);
219+
$this->start();
218220
}
219221

220222
if ($destroy) {
@@ -314,7 +316,7 @@ public function registerBag(SessionBagInterface $bag)
314316
public function getBag($name)
315317
{
316318
if (!isset($this->bags[$name])) {
317-
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
319+
throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
318320
}
319321

320322
if (!$this->started && $this->saveHandler->isActive()) {

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ public function testRegenerateDestroy()
123123
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
124124
}
125125

126+
public function testRegenerateWithCustomLifetime()
127+
{
128+
$storage = $this->getStorage();
129+
$storage->start();
130+
$id = $storage->getId();
131+
$lifetime = 999999;
132+
$storage->getBag('attributes')->set('legs', 11);
133+
$storage->regenerate(false, $lifetime);
134+
$this->assertNotEquals($id, $storage->getId());
135+
$this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
136+
$this->assertEquals($lifetime, ini_get('session.cookie_lifetime'));
137+
}
138+
126139
public function testSessionGlobalIsUpToDateAfterIdRegeneration()
127140
{
128141
$storage = $this->getStorage();

0 commit comments

Comments
 (0)
0