Skip to content

Commit a2cfa3b

Browse files
kenjisMGatner
authored andcommitted
fix: support multiple session cookies
1 parent 93abfb1 commit a2cfa3b

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

system/Session/Handlers/DatabaseHandler.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,18 @@ class DatabaseHandler extends BaseHandler
6060
*/
6161
protected $rowExists = false;
6262

63+
/**
64+
* ID prefix for multiple session cookies
65+
*/
66+
protected string $idPrefix;
67+
6368
/**
6469
* @throws SessionException
6570
*/
6671
public function __construct(AppConfig $config, string $ipAddress)
6772
{
6873
parent::__construct($config, $ipAddress);
74+
6975
$this->table = $config->sessionSavePath;
7076

7177
if (empty($this->table)) {
@@ -77,6 +83,9 @@ public function __construct(AppConfig $config, string $ipAddress)
7783
$this->db = Database::connect($this->DBGroup);
7884

7985
$this->platform = $this->db->getPlatform();
86+
87+
// Add sessionCookieName for multiple session cookies.
88+
$this->idPrefix = $config->sessionCookieName . ':';
8089
}
8190

8291
/**
@@ -115,7 +124,7 @@ public function read($id)
115124
$this->sessionID = $id;
116125
}
117126

118-
$builder = $this->db->table($this->table)->where('id', $id);
127+
$builder = $this->db->table($this->table)->where('id', $this->idPrefix . $id);
119128

120129
if ($this->matchIP) {
121130
$builder = $builder->where('ip_address', $this->ipAddress);
@@ -182,7 +191,7 @@ public function write($id, $data): bool
182191

183192
if ($this->rowExists === false) {
184193
$insertData = [
185-
'id' => $id,
194+
'id' => $this->idPrefix . $id,
186195
'ip_address' => $this->ipAddress,
187196
'data' => $this->prepareData($data),
188197
];
@@ -197,7 +206,7 @@ public function write($id, $data): bool
197206
return true;
198207
}
199208

200-
$builder = $this->db->table($this->table)->where('id', $id);
209+
$builder = $this->db->table($this->table)->where('id', $this->idPrefix . $id);
201210

202211
if ($this->matchIP) {
203212
$builder = $builder->where('ip_address', $this->ipAddress);
@@ -242,7 +251,7 @@ public function close(): bool
242251
public function destroy($id): bool
243252
{
244253
if ($this->lock) {
245-
$builder = $this->db->table($this->table)->where('id', $id);
254+
$builder = $this->db->table($this->table)->where('id', $this->idPrefix . $id);
246255

247256
if ($this->matchIP) {
248257
$builder = $builder->where('ip_address', $this->ipAddress);

system/Session/Handlers/MemcachedHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function __construct(AppConfig $config, string $ipAddress)
6161
throw SessionException::forEmptySavepath();
6262
}
6363

64+
// Add sessionCookieName for multiple session cookies.
65+
$this->keyPrefix .= $config->sessionCookieName . ':';
66+
6467
if ($this->matchIP === true) {
6568
$this->keyPrefix .= $this->ipAddress . ':';
6669
}

system/Session/Handlers/RedisHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public function __construct(AppConfig $config, string $ipAddress)
7171

7272
$this->setSavePath();
7373

74+
// Add sessionCookieName for multiple session cookies.
75+
$this->keyPrefix .= $config->sessionCookieName . ':';
76+
7477
if ($this->matchIP === true) {
7578
$this->keyPrefix .= $this->ipAddress . ':';
7679
}

0 commit comments

Comments
 (0)