Skip to content

Commit 2559b98

Browse files
authored
Merge pull request #4 from blitz-php/devs
fix: correction du bug lié à la génération de la session
2 parents 5382354 + 3729074 commit 2559b98

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

Session.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
use Psr\Log\LoggerAwareTrait;
3030
use RuntimeException;
3131

32+
/**
33+
* Implementation du conteneur de session BlitzPHP.
34+
*
35+
* La configuration de session est faite via les variables session et cookie relatives.
36+
*
37+
* @property string $session_id
38+
*
39+
* @credit <a href="https://codeigniter.com/">CodeIgniter - CodeIgniter\Session\Session</a>
40+
*/
3241
class Session implements SessionInterface
3342
{
3443
use LoggerAwareTrait;
@@ -70,7 +79,7 @@ class Session implements SessionInterface
7079
];
7180

7281
/**
73-
* Adapter a utiliser pour la session
82+
* Instance du pilote à utiliser.
7483
*/
7584
private BaseHandler $adapter;
7685

@@ -316,7 +325,7 @@ protected function sanitizeSessionCookie(): void
316325
$cookieName = $this->config['cookie_name'];
317326

318327
if (isset($_COOKIE[$cookieName])
319-
&& (! is_string($_COOKIE[$cookieName]) || preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$cookieName])) !== 1) {
328+
&& (! is_string($_COOKIE[$cookieName]) || preg_match('#\A' . $this->sidRegexp . '\z#', $_COOKIE[$cookieName]) !== 1)) {
320329
unset($_COOKIE[$cookieName]);
321330
$this->logMessage('Session: Cookie de session invalide détecté et supprimé.', 'warning');
322331
}
@@ -335,10 +344,11 @@ protected function handleSessionRegeneration(): void
335344
$regenerateTime = $this->config['time_to_update'] ?? 300;
336345

337346
if ($regenerateTime > 0) {
338-
$now = Date::now()->getTimestamp();
339-
$lastRegenerate = $_SESSION['__blitz_last_regenerate'] ?? 0;
347+
$now = Date::now()->getTimestamp();
340348

341-
if (($now - $lastRegenerate) >= $regenerateTime) {
349+
if (! isset($_SESSION['__blitz_last_regenerate'])) {
350+
$_SESSION['__blitz_last_regenerate'] = $now;
351+
} elseif ($_SESSION['__blitz_last_regenerate'] < ($now - $regenerateTime)) {
342352
$this->regenerate((bool) $this->config['regenerate_destroy']);
343353
}
344354
}
@@ -369,7 +379,27 @@ public function regenerate(bool $destroy = false): void
369379
session_regenerate_id($destroy);
370380
}
371381

372-
// $this->removeOldSessionCookie();
382+
$this->removeOldSessionCookie();
383+
}
384+
385+
private function removeOldSessionCookie(): void
386+
{
387+
if (! function_exists('service')) {
388+
return;
389+
}
390+
391+
$cookieCollection = service('response')->getCookieCollection();
392+
393+
if (! $cookieCollection->has($this->config['cookie_name'])) {
394+
return;
395+
}
396+
397+
// CookieCollection est immutable.
398+
$newCookieCollection = $cookieCollection->remove($this->config['cookie_name']);
399+
400+
foreach ($newCookieCollection as $cookie) {
401+
setcookie($cookie->getName(), $cookie->getScalarValue(), $cookie->getOptions());
402+
}
373403
}
374404

375405
/**
@@ -789,6 +819,8 @@ protected function setCookie()
789819
{
790820
$expiration = $this->config['expiration'] === 0 ? 0 : Date::now()->getTimestamp() + $this->config['expiration'];
791821
$this->cookie = $this->cookie->withValue(session_id())->withExpiry(Date::createFromTimestamp($expiration));
822+
823+
setcookie($this->cookie->getName(), $this->cookie->getScalarValue(), $this->cookie->getOptions());
792824
}
793825

794826
/**

Store.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace BlitzPHP\Session;
1313

14+
use BlitzPHP\Traits\Macroable;
1415
use BlitzPHP\Utilities\Helpers;
1516
use BlitzPHP\Utilities\Iterable\Arr;
1617
use BlitzPHP\Utilities\String\Text;
@@ -19,6 +20,8 @@
1920

2021
class Store extends Session
2122
{
23+
use Macroable;
24+
2225
/**
2326
* {@inheritDoc}
2427
*/

0 commit comments

Comments
 (0)