-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 564c0aa
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.idea/ | ||
/.vscode/ | ||
/.vs/ | ||
/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 InitPHP | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# InitPHP Redis Session Handler | ||
|
||
This library provides a way to keep your application's sessions on redis, not on the filesystem. | ||
|
||
## Requirements | ||
|
||
- PHP 7.4 or later | ||
- PHP Redis Extension | ||
- [InitPHP Redis Library](https://github.com/InitPHP/Redis) | ||
|
||
## Installation | ||
|
||
``` | ||
composer require initphp/redis-session-handler | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
require_once "vendor/autoload.php"; | ||
|
||
$redis = new \InitPHP\Redis\Redis([ | ||
'host' => '127.0.0.1', | ||
'password' => null, | ||
'port' => 6379, | ||
'timeout' => 0, | ||
'database' => 0, | ||
]); | ||
|
||
$sessionHandler = new InitPHP\RedisSessionHandler\Handler($redis); | ||
session_set_save_handler($sessionHandler, true); | ||
session_start(); | ||
|
||
// You can use the $_SESSION global. | ||
``` | ||
|
||
## Credits | ||
|
||
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>> | ||
|
||
## License | ||
|
||
Copyright © 2022 [MIT License](./LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "initphp/redis-session-handler", | ||
"description": "PHP Redis Session Handler", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"InitPHP\\RedisSessionHandler\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Muhammet ŞAFAK", | ||
"email": "[email protected]", | ||
"role": "Developer", | ||
"homepage": "https://www.muhammetsafak.com.tr" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=7.4", | ||
"ext-redis": "*", | ||
"initphp/redis": "^1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* InitPHP/RedisSessionHandler | ||
* | ||
* This file is part of InitPHP Redis Session Handler. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 1.0 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
namespace InitPHP\RedisSessionHandler; | ||
|
||
use InitPHP\Redis\Redis; | ||
|
||
class Handler implements \SessionHandlerInterface | ||
{ | ||
|
||
private Redis $redis; | ||
|
||
private string $prefix = 'sess_'; | ||
|
||
public function __construct(Redis $redis) | ||
{ | ||
$this->redis = $redis; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function close() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function destroy($id) | ||
{ | ||
$key = $this->prefix . $id; | ||
try { | ||
if($this->redis->has($key)){ | ||
$this->redis->delete($key); | ||
} | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function gc($max_lifetime) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function open($path, $name) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function read($id) | ||
{ | ||
return (string)$this->redis->get($this->prefix . $id, ''); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function write($id, $data) | ||
{ | ||
return $this->redis->set($this->prefix . $id, $data) !== FALSE; | ||
} | ||
|
||
} |