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

Commit 8552a16

Browse files
committed
Merge branch 'hotfix/85'
Close #85
2 parents 01b228b + 053fca6 commit 8552a16

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#85](https://github.com/zendframework/zend-session/pull/85) fixes an issue
26+
with how the expiration seconds are handled when a long-running request
27+
occurs. Previously, when called, it would use the value of
28+
`$_SERVER['REQUEST_TIME']` to calculate the expiration time; this would cause
29+
failures if the expiration seconds had been reached by the time the value was
30+
set. It now correctly uses the current `time()`.
31+
2532
- [#99](https://github.com/zendframework/zend-session/pull/99) fixes how
2633
`Zend\Session\Config\SessionConfig` handles attaching save handlers to ensure
2734
it will honor any handlers registered with the PHP engine (e.g., redis,

src/AbstractContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function getIterator()
509509
public function setExpirationSeconds($ttl, $vars = null)
510510
{
511511
$storage = $this->getStorage();
512-
$ts = $_SERVER['REQUEST_TIME'] + $ttl;
512+
$ts = time() + $ttl;
513513
if (is_scalar($vars) && null !== $vars) {
514514
$vars = (array) $vars;
515515
}

test/ContainerTest.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,24 @@ public function testContainerWritesToStorage()
193193

194194
public function testSettingExpirationSecondsUpdatesStorageMetadataForFullContainer()
195195
{
196+
$currentTimestamp = time();
196197
$this->container->setExpirationSeconds(3600);
197198
$storage = $this->manager->getStorage();
198199
$metadata = $storage->getMetadata($this->container->getName());
199200
$this->assertArrayHasKey('EXPIRE', $metadata);
200-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE']);
201+
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE']);
201202
}
202203

203204
public function testSettingExpirationSecondsForIndividualKeyUpdatesStorageMetadataForThatKey()
204205
{
205206
$this->container->foo = 'bar';
207+
$currentTimestamp = time();
206208
$this->container->setExpirationSeconds(3600, 'foo');
207209
$storage = $this->manager->getStorage();
208210
$metadata = $storage->getMetadata($this->container->getName());
209211
$this->assertArrayHasKey('EXPIRE_KEYS', $metadata);
210212
$this->assertArrayHasKey('foo', $metadata['EXPIRE_KEYS']);
211-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE_KEYS']['foo']);
213+
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE_KEYS']['foo']);
212214
}
213215

214216
public function testMultipleCallsToExpirationSecondsAggregates()
@@ -217,15 +219,29 @@ public function testMultipleCallsToExpirationSecondsAggregates()
217219
$this->container->bar = 'baz';
218220
$this->container->baz = 'bat';
219221
$this->container->bat = 'bas';
222+
$currentTimestamp = time();
220223
$this->container->setExpirationSeconds(3600);
221224
$this->container->setExpirationSeconds(1800, 'foo');
222225
$this->container->setExpirationSeconds(900, ['baz', 'bat']);
223226
$storage = $this->manager->getStorage();
224227
$metadata = $storage->getMetadata($this->container->getName());
225-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 1800, $metadata['EXPIRE_KEYS']['foo']);
226-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 900, $metadata['EXPIRE_KEYS']['baz']);
227-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 900, $metadata['EXPIRE_KEYS']['bat']);
228-
$this->assertEquals($_SERVER['REQUEST_TIME'] + 3600, $metadata['EXPIRE']);
228+
$this->assertEquals($currentTimestamp + 1800, $metadata['EXPIRE_KEYS']['foo']);
229+
$this->assertEquals($currentTimestamp + 900, $metadata['EXPIRE_KEYS']['baz']);
230+
$this->assertEquals($currentTimestamp + 900, $metadata['EXPIRE_KEYS']['bat']);
231+
$this->assertEquals($currentTimestamp + 3600, $metadata['EXPIRE']);
232+
}
233+
234+
public function testSettingExpirationSecondsUsesCurrentTime()
235+
{
236+
sleep(3);
237+
$this->container->setExpirationSeconds(2);
238+
$this->container->foo = 'bar';
239+
240+
// Simulate a second request: overwrite the request time with current time()
241+
$_SERVER['REQUEST_TIME'] = time();
242+
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
243+
244+
$this->assertEquals('bar', $this->container->foo);
229245
}
230246

231247
public function testPassingUnsetKeyToSetExpirationSecondsDoesNothing()

0 commit comments

Comments
 (0)