PHPackages                             bryanjhv/slim-session - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bryanjhv/slim-session

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bryanjhv/slim-session
=====================

Session middleware and helper for Slim framework 4.

4.1.2(4y ago)233961.5k—9.3%3616MITPHPCI failing

Since Jun 26Pushed 2y ago8 watchersCompare

[ Source](https://github.com/bryanjhv/slim-session)[ Packagist](https://packagist.org/packages/bryanjhv/slim-session)[ Docs](https://github.com/bryanjhv/slim-session)[ Fund](https://paypal.me/bryanjhv)[ RSS](/packages/bryanjhv-slim-session/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (42)Used By (16)

slim-session
============

[](#slim-session)

Simple middleware for [Slim Framework 4](https://www.slimframework.com/docs/v4/), 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
------------

[](#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
-----

[](#usage)

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

### Supported options

[](#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](https://www.php.net/manual/en/session.configuration.php). 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
--------------

[](#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
------------

[](#contributors)

[Here](https://github.com/bryanjhv/slim-session/graphs/contributors) are the big ones listed. 😄

TODO
----

[](#todo)

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

License
-------

[](#license)

MIT

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity57

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 78.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~71 days

Total

39

Last Release

1497d ago

Major Versions

2.2.3 → 3.6.52020-05-27

3.6.5 → 4.0.32020-05-27

2.3.0 → 3.7.12021-07-02

3.7.1 → 4.1.12021-07-02

2.3.1 → 4.1.22022-04-13

### Community

Maintainers

![](https://www.gravatar.com/avatar/c148546470b96cf623c5f78b5a97df531334c44c55995b83b5dee4412b46f7ec?d=identicon)[bryanjhv](/maintainers/bryanjhv)

---

Top Contributors

[![bryanjhv](https://avatars.githubusercontent.com/u/2767941?v=4)](https://github.com/bryanjhv "bryanjhv (40 commits)")[![Zemistr](https://avatars.githubusercontent.com/u/2613208?v=4)](https://github.com/Zemistr "Zemistr (3 commits)")[![mohammad-erdin](https://avatars.githubusercontent.com/u/656852?v=4)](https://github.com/mohammad-erdin "mohammad-erdin (3 commits)")[![Farkie](https://avatars.githubusercontent.com/u/294921?v=4)](https://github.com/Farkie "Farkie (1 commits)")[![safronizator](https://avatars.githubusercontent.com/u/1083957?v=4)](https://github.com/safronizator "safronizator (1 commits)")[![tbela99](https://avatars.githubusercontent.com/u/58517?v=4)](https://github.com/tbela99 "tbela99 (1 commits)")[![tersmitten](https://avatars.githubusercontent.com/u/3392962?v=4)](https://github.com/tersmitten "tersmitten (1 commits)")[![LucasPadilha](https://avatars.githubusercontent.com/u/6677271?v=4)](https://github.com/LucasPadilha "LucasPadilha (1 commits)")

---

Tags

middlewarephpsessionslim-3middlewarehelperslimsession

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bryanjhv-slim-session/health.svg)

```
[![Health](https://phpackages.com/badges/bryanjhv-slim-session/health.svg)](https://phpackages.com/packages/bryanjhv-slim-session)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[beste/json

A simple JSON helper to decode and encode JSON

4222.7M3](/packages/beste-json)[chillerlan/php-settings-container

A container class for immutable settings objects. Not a DI container.

3427.3M21](/packages/chillerlan-php-settings-container)[akrabat/rka-slim-session-middleware

Simple session middleware for Slim Framework

45261.6k3](/packages/akrabat-rka-slim-session-middleware)[adbario/slim-secure-session-middleware

Secure session middleware for Slim 3 framework

2932.3k2](/packages/adbario-slim-secure-session-middleware)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
