Skip to content

Commit 58a982e

Browse files
committed
allow to set cookie domain
1 parent aeec8b4 commit 58a982e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ $handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret ke
6868
$handler->replaceSessionHandler(true);
6969
```
7070

71+
### Create the handler and replace the session handler, specifying cookie domain valid for all subdomains of mydomain.com
72+
73+
```php
74+
<?php
75+
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key', null, null, '.mydomain.com');
76+
$handler->replaceSessionHandler(true);
77+
```
78+
7179
### How it works
7280

7381
We store a cookie named AUTH_BEARER_<context name> with the session name. The PHPSESSID cookie is still created because

src/JwtSession.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@ class JwtSession implements SessionHandlerInterface
2222

2323
protected $suffix = "default";
2424

25+
protected $cookieDomain;
26+
2527
/**
2628
* JwtSession constructor.
2729
*
2830
* @param $serverName
2931
* @param $secretKey
3032
* @param int $timeOutMinutes
3133
*/
32-
public function __construct($serverName, $secretKey, $timeOutMinutes = 20, $sessionContext = 'default')
34+
public function __construct($serverName, $secretKey, $timeOutMinutes = 20, $sessionContext = 'default', $cookieDomain = null)
3335
{
3436
$this->serverName = $serverName;
3537
$this->secretKey = $secretKey;
3638
$this->timeOutMinutes = $timeOutMinutes;
3739
$this->suffix = $sessionContext;
40+
$this->cookieDomain = $cookieDomain;
3841
}
3942

4043
public function replaceSessionHandler($startSession = true)
@@ -174,7 +177,7 @@ public function write($session_id, $session_data)
174177
$token = $jwt->generateToken($data);
175178

176179
if (!headers_sent()) {
177-
setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
180+
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, null, $this->cookieDomain);
178181
}
179182

180183
return true;

0 commit comments

Comments
 (0)