8000 bug #12961 fix session restart on PHP 5.3 (Tobion) · symfony/symfony@e38b8e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e38b8e6

Browse files
committed
bug #12961 fix session restart on PHP 5.3 (Tobion)
This PR was merged into the 2.3 branch. Discussion ---------- fix session restart on PHP 5.3 | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | #12562 | License | MIT | Doc PR | - this also removes some useless code Commits ------- b9d3c92 fix session restart on PHP 5.3
2 parents 39a3379 + b9d3c92 commit e38b8e6

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function setSessionData(array $array)
8989
*/
9090< 8000 /code>
public function start()
9191
{
92-
if ($this->started && !$this->closed) {
92+
if ($this->started) {
9393
return true;
9494
}
9595

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ public function getSaveHandler()
126126
*/
127127
public function start()
128128
{
129-
if ($this->started && !$this->closed) {
129+
if ($this->started) {
130130
return true;
131131
}
132132

133133
if (PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
134134
throw new \RuntimeException('Failed to start the session: already started by PHP.');
135135
}
136136

137-
if (PHP_VERSION_ID < 50400 && isset($_SESSION) && session_id()) {
137+
if (PHP_VERSION_ID < 50400 && !$this->closed && isset($_SESSION) && session_id()) {
138138
// not 100% fool-proof, but is the most reliable way to determine if a session is active in PHP 5.3
139139
throw new \RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
140140
}
@@ -162,10 +162,6 @@ public function start()
162162
*/
163163
public function getId()
164164
{
165-
if (!$this->started && !$this->closed) {
166-
return ''; // returning empty is consistent with session_id() behaviour
167-
}
168-
169165
return $this->saveHandler->getId();
170166
}
171167

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($handler = null, MetadataBag $metaBag = null)
3838
*/
3939
public function start()
4040
{
41-
if ($this->started && !$this->closed) {
41+
if ($this->started) {
4242
return true;
4343
}
4444

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ public function testRegisterBagException()
8585
public function testGetId()
8686
{
8787
$storage = $this->getStorage();
88-
$this->assertEquals('', $storage->getId());
88+
$this->assertSame('', $storage->getId(), 'Empty ID before starting session');
8989

9090
$storage->start();
91-
$this->assertNotEquals('', $storage->getId());
91+
$id = $storage->getId();
92+
$this->assertInternalType('string', $id);
93+
$this->assertNotSame('', $id);
9294

9395
$storage->save();
94-
$this->assertNotEquals('', $storage->getId());
96+
$this->assertSame($id, $storage->getId(), 'ID stays after saving session');
9597
}
9698

9799
public function testRegenerate()
@@ -209,18 +211,20 @@ public function testSetSaveHandler54()
209211
/**
210212
* @expectedException \RuntimeException
211213
*/
212-
public function testStartedOutside53()
214+
public function testStartedOutside()
213215
{
214-
if (PHP_VERSION_ID >= 50400) {
215-
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
216-
}
217-
218216
$storage = $this->getStorage();
219217

220218
$this->assertFalse(isset($_SESSION));
219+
$this->assertFalse($storage->getSaveHandler()->isActive());
220+
$this->assertFalse($storage->isStarted());
221221

222222
session_start();
223223
$this->assertTrue(isset($_SESSION));
224+
if (PHP_VERSION_ID >= 50400) {
225+
// this only works in PHP >= 5.4 where session_status is available
226+
$this->assertTrue($storage->getSaveHandler()->isActive());
227+
}
224228
// PHP session might have started, but the storage driver has not, so false is correct here
225229
$this->assertFalse($storage->isStarted());
226230

@@ -229,29 +233,15 @@ public function testStartedOutside53()
229233
$storage->start();
230234
}
231235

232-
/**
233-
* @expectedException \RuntimeException
234-
*/
235-
public function testCanStartOutside54()
236+
public function testRestart()
236237
{
237-
if (PHP_VERSION_ID < 50400) {
238-
$this->markTestSkipped('Test skipped, for PHP 5.4 only.');
239-
}
240-
241238
$storage = $this->getStorage();
242-
243-
$this->assertFalse(isset($_SESSION));
244-
$this->assertFalse($storage->getSaveHandler()->isActive());
245-
$this->assertFalse($storage->isStarted());
246-
247-
session_start();
248-
$this->assertTrue(isset($_SESSION));
249-
$this->assertTrue($storage->getSaveHandler()->isActive());
250-
// PHP session might have started, but the storage driver has not, so false is correct here
251-
$this->assertFalse($storage->isStarted());
252-
253-
$key = $storage->getMetadataBag()->getStorageKey();
254-
$this->assertFalse(isset($_SESSION[$key]));
255239
$storage->start();
240+
$id = $storage->getId();
241+
$storage->getBag('attributes')->set('lucky', 7);
242+
$storage->save();
243+
$storage->start();
244+
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
245+
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
256246
}
257247
}

0 commit comments

Comments
 (0)
0