All Projects → bryanjhv → Slim Session

bryanjhv / Slim Session

Licence: mit
A very simple session middleware for Slim Framework 2/3/4.

Projects that are alternatives of or similar to Slim Session

Session
Simple session middleware for Express
Stars: ✭ 5,571 (+2685.5%)
Mutual labels:  middleware, session
Cookie Session
Simple cookie-based session middleware
Stars: ✭ 928 (+364%)
Mutual labels:  middleware, session
Next Session
Simple promise-based session middleware for Next.js, micro, Express, and more
Stars: ✭ 161 (-19.5%)
Mutual labels:  middleware, session
Laravel Authorize
A middleware to check authorization
Stars: ✭ 179 (-10.5%)
Mutual labels:  middleware
Middy
🛵 The stylish Node.js middleware engine for AWS Lambda
Stars: ✭ 2,592 (+1196%)
Mutual labels:  middleware
Middleware
Community Middleware List for the Iris Web Framework.
Stars: ✭ 188 (-6%)
Mutual labels:  middleware
Webpack Dev Middleware
A development middleware for webpack
Stars: ✭ 2,336 (+1068%)
Mutual labels:  middleware
Kemalyst
A rails like framework based on kemal
Stars: ✭ 176 (-12%)
Mutual labels:  middleware
Botbuilder Community Dotnet
Part of the Bot Builder Community Project. Repository for extensions for the Bot Builder .NET SDK, including middleware, dialogs, recognizers and more.
Stars: ✭ 196 (-2%)
Mutual labels:  middleware
Aura.session
Tools for managing sessions, including session segments and read-once messages
Stars: ✭ 185 (-7.5%)
Mutual labels:  session
Ego
Ego is a full-stack web framework written in Go, lightweight and efficient front-end component solutions, based on gin. The front-end is compiled, does not affect the back-end.
Stars: ✭ 185 (-7.5%)
Mutual labels:  middleware
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (-8.5%)
Mutual labels:  middleware
Netflux
JavaScript client and server side transport API based on WebRTC & WebSocket
Stars: ✭ 188 (-6%)
Mutual labels:  middleware
Micro Aws Lambda
A 7KB and 0 dependencies AWS Lambda library which supports middleware and easy debug.
Stars: ✭ 181 (-9.5%)
Mutual labels:  middleware
Alice
Painless middleware chaining for Go
Stars: ✭ 2,438 (+1119%)
Mutual labels:  middleware
Csurf
CSRF token middleware
Stars: ✭ 2,183 (+991.5%)
Mutual labels:  middleware
Laravel Shield
A HTTP basic auth middleware for Laravel
Stars: ✭ 193 (-3.5%)
Mutual labels:  middleware
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (-8%)
Mutual labels:  middleware
Http Cache
High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs
Stars: ✭ 184 (-8%)
Mutual labels:  middleware
Lightify
a reverse proxy that boosts the web app performance!
Stars: ✭ 187 (-6.5%)
Mutual labels:  middleware

slim-session

Simple middleware for Slim Framework 4, that allows managing PHP built-in sessions and includes a Helper class to help you with the $_SESSION superglobal.

For the middleware version for Slim Framework 3, please check out the slim-3 branch in this repository.

For the middleware version for Slim Framework 2, please check out the slim-2 branch in this repository.

Installation

Add this line to require block in your composer.json:

"bryanjhv/slim-session": "~4.0"

Or, run in a shell instead:

composer require bryanjhv/slim-session:~4.0

Usage

$app = \Slim\Factory\AppFactory::create();
$app->add(
  new \Slim\Middleware\Session([
    'name' => 'dummy_session',
    'autorefresh' => true,
    'lifetime' => '1 hour',
  ])
);

Supported options

  • lifetime: How much should the session last? Default 20 minutes. Any argument that strtotime can parse is valid.
  • path, domain, secure, httponly, samesite: Options for the session cookie. Please note that samesite is 'Lax' by default, set to '' to disable.
  • name: Name for the session cookie. Defaults to slim_session (instead of PHP's PHPSESSID).
  • autorefresh: true if you want session to be refresh when user activity is made (interaction with server).
  • handler: Custom session handler class or object. Must implement SessionHandlerInterface as required by PHP.
  • ini_settings: Associative array of custom session configuration. Previous versions of this package had some hardcoded values which could bring serious performance leaks (see #30):
    [
      'session.gc_divisor' => 1,
      'session.gc_probability' => 1,
      'session.gc_maxlifetime' => 30 * 24 * 60 * 60,
    ];
    

Session helper

A Helper class is available, which you can register globally or instantiate:

$container = new \DI\Container();

// Register globally to app
$container->set('session', function () {
  return new \SlimSession\Helper();
});
\Slim\Factory\AppFactory::setContainer($container);

That will provide $app->get('session'), so you can do:

$app->get('/', function ($req, $res) {
  // or $this->get('session') if registered
  $session = new \SlimSession\Helper();

  // Check if variable exists
  $exists = $session->exists('my_key');
  $exists = isset($session->my_key);
  $exists = isset($session['my_key']);

  // Get variable value
  $my_value = $session->get('my_key', 'default');
  $my_value = $session->my_key;
  $my_value = $session['my_key'];

  // Set variable value
  $app->get('session')->set('my_key', 'my_value');
  $session->my_key = 'my_value';
  $session['my_key'] = 'my_value';

  // Merge value recursively
  $app->get('session')->merge('my_key', ['first' => 'value']);
  $session->merge('my_key', ['second' => ['a' => 'A']]);
  $letter_a = $session['my_key']['second']['a']; // "A"

  // Delete variable
  $session->delete('my_key');
  unset($session->my_key);
  unset($session['my_key']);

  // Destroy session
  $session::destroy();

  // Get session id
  $id = $this->session::id();

  return $res;
});

Contributors

Here are the big ones listed. 😄

TODO

  • Complete Helper tests. (thanks @Zemistr)
  • Slim-specific tests (integration with Slim App).

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].