Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Nov 6, 2022
0 parents commit 564c0aa
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.idea/
/.vscode/
/.vs/
/vendor/
/composer.lock
21 changes: 21 additions & 0 deletions LICENSE
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.
43 changes: 43 additions & 0 deletions README.md
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 &copy; 2022 [MIT License](./LICENSE)
25 changes: 25 additions & 0 deletions composer.json
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"
}
}
86 changes: 86 additions & 0 deletions src/Handler.php
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;
}

}

0 comments on commit 564c0aa

Please sign in to comment.