Skip to content

Instantly share code, notes, and snippets.

@GeertHauwaerts
Last active July 29, 2019 13:14
Show Gist options
  • Save GeertHauwaerts/338dabe9fced0c45c3c6ccda65206c68 to your computer and use it in GitHub Desktop.
Save GeertHauwaerts/338dabe9fced0c45c3c6ccda65206c68 to your computer and use it in GitHub Desktop.
Slim3 - PHP sessions with Eloquent backend
<?php
use Dotenv\Dotenv;
use Illuminate\Database\Capsule\Manager as Capsule;
$dotenv = Dotenv::create(__DIR__ . '/../');
$dotenv->load();
$settings = require __DIR__ . '/settings.php';
$capsule = new Capsule;
$capsule->addConnection($settings['settings']['mysql']);
$capsule->setAsGlobal();
$capsule->bootEloquent();
<?php
namespace App\Helpers;
use App\Models\Session;
use Carbon\Carbon;
use Illuminate\Database\Capsule\Manager as Capsule;
class SessionHandler implements \SessionHandlerInterface
{
private $isLocked;
private $lifetime;
public function __construct($lifetime = '1 day')
{
$this->isLocked = false;
$this->lifetime = $lifetime;
if (is_string($this->lifetime)) {
$this->lifetime = strtotime($this->lifetime) - time();
}
}
private function lock()
{
if (!$this->isLocked) {
Capsule::statement('SELECT GET_LOCK("' . session_id() . '", 120)');
}
$this->isLocked = true;
}
private function unlock()
{
if ($this->isLocked) {
Capsule::statement('SELECT RELEASE_LOCK("' . session_id() . '")');
}
$this->isLocked = false;
}
public function open($savePath, $sessionName)
{
Session::where(
'updated_at',
'<',
Carbon::now()->subSeconds($this->lifetime)
)->delete();
$this->lock();
return true;
}
public function close()
{
$this->unlock();
return true;
}
public function read($id)
{
$this->lock();
$result = Session::where('id', '=', $id)->first();
if (!isset($result->id)) {
return '';
}
return $result->data;
}
public function write($id, $data)
{
$this->lock();
Session::updateOrCreate(
['id' => $id],
['data' => $data]
);
return true;
}
public function destroy($id)
{
Session::destroy($id);
$this->unlock();
return true;
}
public function gc($maxlifetime)
{
return true;
}
}
<?php
use Anddye\Middleware\SessionMiddleware;
use App\Helpers\SessionHandler;
use Slim\App;
return function (App $app) {
$handler = null;
if (php_sapi_name() === 'fpm-fcgi') {
$handler = new SessionHandler('1 month');
}
$app->add(new SessionMiddleware([
'autorefresh' => true,
'lifetime' => '1 month',
'handler' => $handler,
]));
};
<?php
namespace App\Migrations;
use Illuminate\Database\Capsule\Manager as Capsule;
class Migration20190725002 extends Migrations
{
protected $num;
public function __construct($num)
{
$this->num = $num;
}
public function run()
{
if ($this->skip()) {
return false;
}
Capsule::schema()->create('sessions', function ($table) {
$table->string('id', 32)->unique();
$table->mediumText('data', 32);
$table->timestamps();
});
$this->finish();
return true;
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Session extends Eloquent
{
public $incrementing = false;
protected $table = 'sessions';
protected $fillable = [
'id',
'data',
];
}
$ composer require slim/slim
$ composer require nesbot/carbon
$ composer require illuminate/database
$ composer require andrewdyer/slim3-session-middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment