Initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/nbproject/
|
||||
@@ -0,0 +1,38 @@
|
||||
# Kavalanche/SqlSessionHandler
|
||||
SQL session handler for PHP
|
||||
|
||||
## Usage
|
||||
|
||||
1. Require `kavalanche/sql-session-handler`.
|
||||
|
||||
```bash
|
||||
composer require kavalanche/sql-session-handler
|
||||
```
|
||||
|
||||
2. Create instance of `Kavalanche\SqlSessionHandler\SqlSessionHandler` and inject your database handle (PDO) into it.
|
||||
|
||||
```php
|
||||
$SqlSessionHandler = new Kavalanche\SqlSessionHandler\SqlSessionHandler($db);
|
||||
session_set_save_handler($SqlSessionHandler);
|
||||
session_start();
|
||||
```
|
||||
|
||||
## Database structure
|
||||
|
||||
```sql
|
||||
CREATE TABLE `session` (
|
||||
`id` char(32) CHARACTER SET ascii NOT NULL,
|
||||
`timestamp` int(10) unsigned NOT NULL,
|
||||
`data` longtext NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
You can change session table name in `config/sql-session-handler.yaml` placed in the root of your application.
|
||||
|
||||
Default value is:
|
||||
```yaml
|
||||
session-table-name: session
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "kavalanche/sql-session-handler",
|
||||
"description": "SQL session handler for PHP",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Wojtek",
|
||||
"email": "w.lk@wp.pl"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kavalanche\\SqlSessionHandler\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"config/config.php"
|
||||
]
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Kavalanche\SqlSessionHandler;
|
||||
|
||||
function config(string $key) {
|
||||
$config = yaml_parse_file(__DIR__ . '/parameters.yaml');
|
||||
$userConfigPath = __DIR__ . '/../../../../config/sql-session-handler.yaml';
|
||||
if (file_exists($userConfigPath)) {
|
||||
$userConfig = yaml_parse_file($userConfigPath);
|
||||
$config = array_merge($config, $userConfig);
|
||||
}
|
||||
|
||||
if (!empty($config[$key])) {
|
||||
return $config[$key];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
session-table-name: session
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Kavalanche\SqlSessionHandler;
|
||||
|
||||
use PDO;
|
||||
use SessionHandlerInterface;
|
||||
use function Kavalanche\SqlSessionHandler\config;
|
||||
|
||||
class SqlSessionHandler implements SessionHandlerInterface {
|
||||
|
||||
private PDO $db;
|
||||
private string $tableName;
|
||||
|
||||
public function __construct(PDO $db) {
|
||||
$this->db = $db;
|
||||
$this->tableName = config('session-table-name');
|
||||
}
|
||||
|
||||
public function close(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function destroy(string $id): bool {
|
||||
$sql = "DELETE FROM " . $this->tableName . " WHERE id = :id";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
return $stmt->execute([
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
|
||||
public function gc(int $max_lifetime): int|false {
|
||||
$sql = "DELETE FROM " . $this->tableName . " WHERE timestamp <= :timestamp";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->execute([
|
||||
'timestamp' => time() - $max_lifetime
|
||||
]);
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
|
||||
public function open(string $path, string $name): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function read(string $id): string|false {
|
||||
$sql = "SELECT data FROM " . $this->tableName . " WHERE id = :id";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->execute([
|
||||
'id' => $id
|
||||
]);
|
||||
|
||||
$data = $stmt->fetch();
|
||||
|
||||
return !empty($data) ? $data['data'] : '';
|
||||
}
|
||||
|
||||
public function write(string $id, string $data): bool {
|
||||
if (empty($this->read($id))) {
|
||||
$sql = "INSERT INTO " . $this->tableName . " (id, data, timestamp) VALUES (:id, :data, :timestamp)";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
return $stmt->execute([
|
||||
'id' => $id,
|
||||
'data' => $data,
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
$sql = "UPDATE " . $this->tableName . " SET data = :data, timestamp = :timestamp WHERE id = :id";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
return $stmt->execute([
|
||||
'id' => $id,
|
||||
'data' => $data,
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user