diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfa762a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea/ +/.vscode/ +/.vs/ +/vendor/ +/composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..568f8f5 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0077099 --- /dev/null +++ b/README.md @@ -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) <> + +## License + +Copyright © 2022 [MIT License](./LICENSE) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..af8cd22 --- /dev/null +++ b/composer.json @@ -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": "info@muhammetsafak.com.tr", + "role": "Developer", + "homepage": "https://www.muhammetsafak.com.tr" + } + ], + "minimum-stability": "stable", + "require": { + "php": ">=7.4", + "ext-redis": "*", + "initphp/redis": "^1.0" + } +} diff --git a/src/Handler.php b/src/Handler.php new file mode 100644 index 0000000..0e493f4 --- /dev/null +++ b/src/Handler.php @@ -0,0 +1,86 @@ + + * @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; + } + +}