Skip to content

Commit

Permalink
Merge pull request #1 from byjg/1.0
Browse files Browse the repository at this point in the history
1.0
  • Loading branch information
byjg authored May 27, 2017
2 parents e5d624d + c4167c6 commit aeec8b4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ session_set_save_handler($handler, true);
$handler = new \ByJG\Session\JwtSession('your.domain.com', 'your super secret key');
$handler->replaceSessionHandler(true);
```

### How it works

We store a cookie named AUTH_BEARER_<context name> with the session name. The PHPSESSID cookie is still created because
PHP create it by default but we do not use it;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "byjg/jwt-session",
"description": "Use JWT Token as a PHP Session",
"description": "JwtSession is a PHP session replacement. Instead of use FileSystem, just use JWT TOKEN. The implementation following the SessionHandlerInterface.",
"authors": [
{
"name": "João Gilberto Magalhães",
Expand Down
9 changes: 7 additions & 2 deletions src/JwtSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public function close()
*/
public function destroy($session_id)
{
setcookie(self::COOKIE_PREFIX . $this->suffix, null);
if (!headers_sent()) {
setcookie(self::COOKIE_PREFIX . $this->suffix, null);
}

return true;
}

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

setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
if (!headers_sent()) {
setcookie(self::COOKIE_PREFIX . $this->suffix, $token);
}

return true;
}
Expand Down
12 changes: 10 additions & 2 deletions webtest/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

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

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

session_start();

?>

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

0 comments on commit aeec8b4

Please sign in to comment.