2929use Psr \Log \LoggerAwareTrait ;
3030use 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+ */
3241class 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 /**
0 commit comments