PHPackages                             bayfrontmedia/session-manager - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. bayfrontmedia/session-manager

ActiveLibrary[File &amp; Storage](/categories/file-storage)

bayfrontmedia/session-manager
=============================

A framework-agnostic PHP library to manage sessions using multiple storage options.

v3.1.2(3mo ago)3251MITPHPPHP ^8.0

Since Sep 14Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/bayfrontmedia/session-manager)[ Packagist](https://packagist.org/packages/bayfrontmedia/session-manager)[ Docs](https://github.com/bayfrontmedia/session-manager)[ RSS](/packages/bayfrontmedia-session-manager/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (1)

Session Manager
---------------

[](#session-manager)

A framework-agnostic PHP library to manage sessions using multiple storage options.

- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)

License
-------

[](#license)

This project is open source and available under the [MIT License](LICENSE).

Author
------

[](#author)

[![Bayfront Media](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)](https://camo.githubusercontent.com/0c0163913b2092e97dbf9684970adaf86f2f25871298d3d28b2dff4bf75b2915/68747470733a2f2f63646e312e6f6e62617966726f6e742e636f6d2f62666d2f6272616e642f62666d2d6c6f676f2e737667)

- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)

Requirements
------------

[](#requirements)

- PHP `^8.0` (Tested up to `8.4`)
- PDO PHP extension

Installation
------------

[](#installation)

```
composer require bayfrontmedia/session-manager
```

Usage
-----

[](#usage)

### Session handler

[](#session-handler)

A `\SessionHandlerInterface` must be passed to the `Bayfront\SessionManager\Session` constructor. There are a variety of session handlers available, each with their own required configuration.

In addition, you may also create and use your own session handlers to be used with Session Manager.

**LocalHandler**

The `LocalHandler` allows you to store sessions in the local filesystem using native PHP.

```
use Bayfront\SessionManager\Handlers\LocalHandler;

$handler = new LocalHandler('/root_path');
```

**PdoHandler**

The `PdoHandler` allows you to use a `PDO` instance for session storage in a database.

```
use Bayfront\SessionManager\Handlers\PdoHandler;

$dbh = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'USERNAME', 'PASSWORD');

// Pass the table name to be used in the constructor - "sessions" by default
$handler = new PdoHandler($dbh, 'sessions');
```

Before using the `PdoHandler`, the required database table must be created with the `up` method, and may throw a `Bayfront\SessionManager\HandlerException` exception:

```
try {
    $handler->up();
} catch (HandlerException $e) {
    die($e->getMessage());
}
```

**PredisHandler**

The `PredisHandler` allows you to use a [Predis](https://github.com/predis/predis) `Client` instance for session storage in Redis.

The constructor requires a `Client` instance, along with the max lifetime value (in seconds). An optional key prefix can also be defined.

```
$client = new Client([
    'scheme' => 'tcp',
    'host' => '10.0.0.1',
    'port' => 6379,
    'tcp_nodelay' => true,
    'persistent' => true,
    'username' => 'USERNAME',
    'password' => 'PASSWORD'
]);

$handler = new PredisHandler($client, 3600, 'prod:session:');
```

NOTE: When using the `PredisHandler`, Redis automatically deletes expired sessions based on the defined max lifetime. Therefore, the `sess_gc_probability` Session config value should be `0` to disable PHP's session garbage collection.

**RedisHandler**

The `RedisHandler` allows you to use a [Redis](https://github.com/phpredis/phpredis) instance using the `redis` PHP extension for session storage in Redis.

The constructor requires a `Redis` instance, along with the max lifetime value (in seconds). An optional key prefix can also be defined.

```
$redis = new Redis();

$redis->pconnect('10.0.0.1', 6379, 2, 'persist_id');

$redis->auth([
    'USERNAME',
    'PASSWORD'
]);

$redis->setOption(Redis::OPT_PREFIX, 'global:prefix:');

$handler = new RedisHandler($redis, 3600, 'prod:session:');
```

NOTE: When using the `RedisHandler`, Redis automatically deletes expired sessions based on the defined max lifetime. Therefore, the `sess_gc_probability` Session config value should be `0` to disable PHP's session garbage collection.

### Start using Session Manager

[](#start-using-session-manager)

Once your handler has been created, it can be used with Session Manager. In addition, a configuration array should be passed to the constructor.

Unless otherwise specified, the default configuration will be used, as shown below:

```
use Bayfront\SessionManager\Handlers\LocalHandler;
use Bayfront\SessionManager\Session;

$handler = new LocalHandler('/root_path');

$config = [
    'cookie_name' => 'bfm_sess',
    'cookie_path' => '/',
    'cookie_domain' => '',
    'cookie_secure' => true,
    'cookie_http_only' => true,
    'cookie_same_site' => 'Lax', // None, Lax or Strict
    'sess_regenerate_duration' => 300, // 0 to disable
    'sess_lifetime' => 3600, // 0 for "until the browser is closed"
    'sess_gc_probability' => 1, // 0 to disable garbage collection
    'sess_gc_divisor' => 100
];

$session = new Session($handler, $config);
```

The `cookie_*` keys allow you to configure the [session cookie parameters](https://www.php.net/manual/en/function.session-set-cookie-params.php).

The `sess_regenerate_duration` key is the number of seconds interval before a new session is automatically created (prevents session fixation). Set to `0` to disable automatically regenerating sessions.

The `sess_lifetime` key is the number of seconds the session will be valid. Set to `0` for the session to be valid only "until the browser is closed".

The `sess_gc_*` keys define the [probability](https://www.php.net/manual/en/session.configuration.php#ini.session.gc-probability) and [divisor](https://www.php.net/manual/en/session.configuration.php#ini.session.gc-divisor) for the garbage cleanup.

**NOTE:** Be sure to call [start](#start) before using any other methods to ensure the session has begun.

### Public methods

[](#public-methods)

- [start](#start)
- [startNew](#startnew)
- [regenerate](#regenerate)
- [destroy](#destroy)
- [getId](#getid)
- [getLastActive](#getlastactive)
- [getLastRegenerate](#getlastregenerate)
- [get](#get)
- [has](#has)
- [set](#set)
- [forget](#forget)
- [flash](#flash)
- [getFlash](#getflash)
- [hasFlash](#hasflash)
- [keepFlash](#keepflash)
- [reflash](#reflash)

---

### start

[](#start)

**Description:**

Start a new session.

**Parameters:**

- None

**Returns:**

- (self)

**Example:**

```
$session->start();
```

---

### startNew

[](#startnew)

**Description:**

Destroy existing and start a new session.

**Parameters:**

- None

**Returns:**

- (self)

**Example:**

```
$session->startNew();
```

---

### regenerate

[](#regenerate)

**Description:**

Regenerate new session ID.

When `$delete_old_session = TRUE`, the old session file will be deleted.

**Parameters:**

- `$delete_old_session = false` (bool)

**Returns:**

- (self)

**Example:**

```
$session->regenerate();
```

---

### destroy

[](#destroy)

**Description:**

Destroy the current session file and cookie.

**Parameters:**

- None

**Returns:**

- (self)

**Example:**

```
$session->destroy();
```

---

### getId

[](#getid)

**Description:**

Return current session ID

**Parameters:**

- None

**Returns:**

- (string)

**Example:**

```
echo $session->getId();
```

---

### getLastActive

[](#getlastactive)

**Description:**

Return the last active time of the session.

**Parameters:**

- None

**Returns:**

- (int)

**Example:**

```
echo $session->getLastActive();
```

---

### getLastRegenerate

[](#getlastregenerate)

**Description:**

Return the last regenerated time of the session.

**Parameters:**

- None

**Returns:**

- (int)

**Example:**

```
echo $session->getLastRegenerate();
```

---

### get

[](#get)

**Description:**

Returns value of single `$_SESSION` array key in dot notation, or entire array, with optional default value.

**Parameters:**

- `$key = NULL` (string): Returns the entire array when `NULL`
- `$default = NULL` (mixed)

**Returns:**

- (mixed)

**Example:**

```
echo $session->get('user.id');
```

---

### has

[](#has)

**Description:**

Checks if `$_SESSION` array key exists in dot notation.

**Parameters:**

- `$key` (string)

**Returns:**

- (bool)

**Example:**

```
if ($session->has('user.id')) {
    // Do something
}
```

---

### set

[](#set)

**Description:**

Sets a value for a `$_SESSION` key in dot notation.

**Parameters:**

- `$key` (string)
- `$value` (mixed)

**Returns:**

- (self)

**Example:**

```
$session->set('user.id', 5);
```

### forget

[](#forget)

**Description:**

Remove a single key, or an array of keys from the `$_SESSION` array using dot notation.

**Parameters:**

- `$keys` (string|array)

**Returns:**

- (self)

**Example:**

```
$session->forget('user.id');
```

---

### flash

[](#flash)

**Description:**

Sets a value for flash data in dot notation.

Flash data is available immediately and during the subsequent request.

**Parameters:**

- `$key` (string)
- `$value` (mixed)

**Returns:**

- (self)

**Example:**

```
$session->flash('status', 'Task was successful');
```

---

### getFlash

[](#getflash)

**Description:**

Returns value of single flash data key in dot notation, or entire array, with optional default value.

**Parameters:**

- `$key = NULL` (string): Returns the entire flash array when `NULL`
- `$default = NULL` (mixed)

**Returns:**

- (self)

**Example:**

```
echo $session->getFlash('status');
```

---

### hasFlash

[](#hasflash)

**Description:**

Checks if flash data key exists in dot notation.

**Parameters:**

- `$key` (string)

**Returns:**

- (bool)

**Example:**

```
if ($session->hasFlash('status')) {
    // Do something
}
```

---

### keepFlash

[](#keepflash)

**Description:**

Keeps specific flash data keys available for the subsequent request.

**Parameters:**

- `$keys` (array)

**Returns:**

- (self)

**Example:**

```
$session->keepFlash([
    'status'
]);
```

---

### reflash

[](#reflash)

**Description:**

Keeps all flash data keys available for the subsequent request.

**Parameters:**

- None

**Returns:**

- (self)

**Example:**

```
$session->reflash();
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance82

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~220 days

Recently: every ~257 days

Total

10

Last Release

91d ago

Major Versions

v1.1.0 → v2.0.02023-01-26

v2.1.0 → v3.0.02023-04-26

PHP version history (3 changes)1.0.0PHP &gt;=7.1.0

v1.1.0PHP &gt;=7.3.0

v2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ad62c8d0e69358fd63b16fdaa71d5359231cd0cf660bbc3419071dc705c63a8?d=identicon)[bayfrontmedia](/maintainers/bayfrontmedia)

---

Top Contributors

[![robinsonjohn](https://avatars.githubusercontent.com/u/24327848?v=4)](https://github.com/robinsonjohn "robinsonjohn (21 commits)")

---

Tags

phpsessionphpsession

### Embed Badge

![Health badge](/badges/bayfrontmedia-session-manager/health.svg)

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

###  Alternatives

[blueimp/jquery-file-upload

File Upload widget for jQuery.

141.5M18](/packages/blueimp-jquery-file-upload)[matecat/simple-s3

Simple S3 Client

1415.0k](/packages/matecat-simple-s3)

PHPackages © 2026

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