Skip to content

Commit b12db86

Browse files
authored
Merge pull request #3 from byjg/1.0.1
1.0.1
2 parents aeec8b4 + 50ceef2 commit b12db86

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ To avoid this you have to create REDIS/MEMCACHED clusters.
2929
But if you save the session into JWT Token you do not need to create a new server.
3030
Just to use.
3131

32+
You can read more in this Codementor's article:
33+
[Using JSON Web Token (JWT) as a PHP Session](https://www.codementor.io/byjg/using-json-web-token-jwt-as-a-php-session-axeuqbg1m)
34+
3235
## Security Information
3336

3437
The JWT Token cannot be changed, but it can be read.
@@ -68,6 +71,14 @@ $handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret ke
6871
$handler->replaceSessionHandler(true);
6972
```
7073

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

7384
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
<?php
2-
/**
3-
* User: jg
4-
* Date: 14/02/17
5-
* Time: 12:52
6-
*/
72

83
namespace ByJG\Session;
94

@@ -22,19 +17,22 @@ class JwtSession implements SessionHandlerInterface
2217

2318
protected $suffix = "default";
2419

20+
protected $cookieDomain;
21+
2522
/**
2623
* JwtSession constructor.
2724
*
2825
* @param $serverName
2926
* @param $secretKey
3027
* @param int $timeOutMinutes
3128
*/
32-
public function __construct($serverName, $secretKey, $timeOutMinutes = 20, $sessionContext = 'default')
29+
public function __construct($serverName, $secretKey, $timeOutMinutes = null, $sessionContext = null, $cookieDomain = null)
3330
{
3431
$this->serverName = $serverName;
3532
$this->secretKey = $secretKey;
36-
$this->timeOutMinutes = $timeOutMinutes;
37-
$this->suffix = $sessionContext;
33+
$this->timeOutMinutes = $timeOutMinutes ?: 20;
34+
$this->suffix = $sessionContext ?: 'default';
35+
$this->cookieDomain = $cookieDomain;
3836
}
3937

4038
public function replaceSessionHandler($startSession = true)
@@ -174,7 +172,7 @@ public function write($session_id, $session_data)
174172
$token = $jwt->generateToken($data);
175173

176174
if (!headers_sent()) {
177-
setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
175+
setcookie(self::COOKIE_PREFIX . $this->suffix, $token, null, '/', $this->cookieDomain);
178176
}
179177

180178
return true;

webtest/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
$handler->replaceSessionHandler(true);
88
} else {
99
echo "<H1>JWT Session is disabled</H1>";
10+
session_start();
1011
}
1112

12-
session_start();
13-
1413
?>
1514

1615
<h1>JwtSession Demo</h1>

0 commit comments

Comments
 (0)