Skip to content

Commit 91909c4

Browse files
authored
Merge pull request #6167 from kenjis/fix-session-cookie-twice-ajax
fix: Session cookies are sent twice with Ajax
2 parents 588ea79 + 548e5e9 commit 91909c4

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

system/Session/Session.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,28 @@ public function regenerate(bool $destroy = false)
411411
{
412412
$_SESSION['__ci_last_regenerate'] = time();
413413
session_regenerate_id($destroy);
414+
415+
$this->removeOldSessionCookie();
416+
}
417+
418+
private function removeOldSessionCookie(): void
419+
{
420+
$response = Services::response();
421+
$cookieStoreInResponse = $response->getCookieStore();
422+
423+
if (! $cookieStoreInResponse->has($this->sessionCookieName)) {
424+
return;
425+
}
426+
427+
// CookieStore is immutable.
428+
$newCookieStore = $cookieStoreInResponse->remove($this->sessionCookieName);
429+
430+
// But clear() method clears cookies in the object (not immutable).
431+
$cookieStoreInResponse->clear();
432+
433+
foreach ($newCookieStore as $cookie) {
434+
$response->setCookie($cookie);
435+
}
414436
}
415437

416438
/**

0 commit comments

Comments
 (0)