Skip to content

Commit aeec8b4

Browse files
authored
Merge pull request #1 from byjg/1.0
1.0
2 parents e5d624d + c4167c6 commit aeec8b4

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ session_set_save_handler($handler, true);
6767
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key');
6868
$handler->replaceSessionHandler(true);
6969
```
70+
71+
### How it works
72+
73+
We store a cookie named AUTH_BEARER_<context name> with the session name. The PHPSESSID cookie is still created because
74+
PHP create it by default but we do not use it;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "byjg/jwt-session",
3-
"description": "Use JWT Token as a PHP Session",
3+
"description": "JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN. The implementation following the SessionHandlerInterface.",
44
"authors": [
55
{
66
"name": "João Gilberto Magalhães",

src/JwtSession.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ public function close()
7979
*/
8080
public function destroy($session_id)
8181
{
82-
setcookie(self::COOKIE_PREFIX . $this->suffix, null);
82+
if (!headers_sent()) {
83+
setcookie(self::COOKIE_PREFIX . $this->suffix, null);
84+
}
85+
8386
return true;
8487
}
8588

@@ -170,7 +173,9 @@ public function write($session_id, $session_data)
170173
$data = $jwt->createJwtData($this->unSerializeSessionData($session_data), $this->timeOutMinutes * 60);
171174
$token = $jwt->generateToken($data);
172175

173-
setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
176+
if (!headers_sent()) {
177+
setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
178+
}
174179

175180
return true;
176181
}

webtest/index.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
require_once __DIR__ . "/../vendor/autoload.php";
44

5-
$handler = new \ByJG\Session\JwtSession('api.com.br', '1234567890');
6-
$handler->replaceSessionHandler(true);
5+
if (!isset($_REQUEST['turnoff'])) { // Just for turnoff the session
6+
$handler = new \ByJG\Session\JwtSession('api.com.br', '1234567890');
7+
$handler->replaceSessionHandler(true);
8+
} else {
9+
echo "<H1>JWT Session is disabled</H1>";
10+
}
11+
12+
session_start();
713

814
?>
915

@@ -25,5 +31,7 @@
2531
<li><a href="setsession.php">Set a session</a></li>
2632
<li><a href="unsetsession.php">Unset a session</a></li>
2733
<li><a href="destroy.php">Destroy all session</a></li>
34+
<li><a href="index.php">Refresh Page</a></li>
35+
<li><a href="index.php?turnoff=true">Turnoff JwtSession</a></li>
2836
</ul>
2937
</div>

0 commit comments

Comments
 (0)