Skip to content

Commit 1523868

Browse files
authored
Merge pull request #2 from mirkogeest/master
Allow to set cookie domain
2 parents aeec8b4 + 02bbc9e commit 1523868

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
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: 7 additions & 4 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 = null, $sessionContext = null, $cookieDomain = null)
3335
{
3436
$this->serverName = $serverName;
3537
$this->secretKey = $secretKey;
36-
$this->timeOutMinutes = $timeOutMinutes;
37-
$this->suffix = $sessionContext;
38+
$this->timeOutMinutes = $timeOutMinutes ?: 20;
39+
$this->suffix = $sessionContext ?: 'default';
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, '/', $this->cookieDomain);
178181
}
179182

180183
return true;

0 commit comments

Comments
 (0)