PHPackages                             laikait/laika-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. [Caching](/categories/caching)
4. /
5. laikait/laika-session

ActiveLibrary[Caching](/categories/caching)

laikait/laika-session
=====================

A php singleton file,pdo,redis or memcached session handler.

v1.1.0(1mo ago)1998↓30%1MITPHPPHP &gt;=8.1CI passing

Since Oct 17Pushed 1mo agoCompare

[ Source](https://github.com/laikait/laika-session)[ Packagist](https://packagist.org/packages/laikait/laika-session)[ RSS](/packages/laikait-laika-session/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)DependenciesVersions (7)Used By (1)

Laika Session
=============

[](#laika-session)

A PHP session package for the Laika Framework supporting File, PDO, Redis, and Memcached backends via a clean static facade.

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

[](#requirements)

- PHP `>= 8.1`
- `ext-pdo` — for PDO driver
- `ext-redis` — for Redis driver
- `ext-memcached` — for Memcached driver

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

[](#installation)

```
composer require laikait/laika-session
```

---

Quick Start
-----------

[](#quick-start)

Call `SessionManager::config()` once at your application bootstrap, before any session reads or writes.

```
use Laika\Session\SessionManager;
use Laika\Session\Session;

// File driver (default — no instance required)
SessionManager::config();

// Write and read
Session::set('user_id', 42);
echo Session::get('user_id'); // 42
```

---

Drivers
-------

[](#drivers)

### File

[](#file)

Stores sessions as files on disk. No dependencies. Suitable for single-server deployments.

```
SessionManager::config(null, [
    'path'   => '/var/www/storage/sessions', // optional, defaults to session_save_path()
    'prefix' => 'LK',                        // optional, default 'LK'
]);
```

### PDO (MySQL)

[](#pdo-mysql)

Stores sessions in a database table. Pass a pre-configured `PDO` instance. The `sessions` table is created automatically on first use.

```
$pdo = new PDO(
    'mysql:host=127.0.0.1;dbname=myapp;charset=utf8mb4',
    'username',
    'password',
    [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
);

SessionManager::config($pdo);
```

**Auto-created table schema:**

```
CREATE TABLE IF NOT EXISTS `sessions` (
    `id`            VARCHAR(128) PRIMARY KEY,
    `data`          BLOB,
    `last_activity` INT
);
```

### Redis

[](#redis)

Pass a connected and authenticated `Redis` instance.

```
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('your-password'); // omit if no auth

SessionManager::config($redis, [
    'prefix'         => 'LK',   // optional, default 'LK'
    'gc_maxlifetime' => 1440,   // optional, seconds — defaults to session.gc_maxlifetime ini
]);
```

### Memcached

[](#memcached)

Pass a configured `Memcached` instance.

```
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);

SessionManager::config($memcached, [
    'prefix'         => 'LK',   // optional, default 'LK'
    'gc_maxlifetime' => 1440,   // optional, seconds — defaults to session.gc_maxlifetime ini
]);
```

---

Configuration
-------------

[](#configuration)

### Session Options

[](#session-options)

Override PHP session options after calling `config()`:

```
SessionManager::setOptions([
    'name'           => 'MY_APP',   // session cookie name, default 'LAIKA'
    'gc_maxlifetime' => 3600,       // session lifetime in seconds, default 1440
    'gc_probability' => 1,
    'gc_divisor'     => 100,
]);
```

### Cookie Parameters

[](#cookie-parameters)

```
SessionManager::setCookies([
    'path'     => '/',
    'domain'   => '.example.com',
    'secure'   => true,     // HTTPS only — default true
    'httponly' => true,     // no JS access — default true
    'samesite' => 'Strict', // default 'Strict'
]);
```

**Default cookie parameters:**

ParameterDefault`path``/``secure``true``httponly``true``samesite``Strict`---

Session API
-----------

[](#session-api)

All methods are static and available on the `Session` facade.

### `Session::set()`

[](#sessionset)

Store one or multiple values. Data is namespaced under a `$for` key (default `APP`).

```
// Single value
Session::set('user_id', 42);

// Multiple values at once
Session::set(['user_id' => 42, 'role' => 'admin']);

// Custom namespace
Session::set('token', 'abc123', 'AUTH');
```

### `Session::get()`

[](#sessionget)

Retrieve a value. Returns `null` if not found.

```
$userId = Session::get('user_id');        // from 'APP' namespace
$token  = Session::get('token', 'AUTH'); // from 'AUTH' namespace
```

### `Session::has()`

[](#sessionhas)

Check if a key exists.

```
if (Session::has('user_id')) {
    // logged in
}
```

### `Session::pop()`

[](#sessionpop)

Remove a key if it exists.

```
Session::pop('flash_message');
Session::pop('token', 'AUTH');
```

### `Session::all()`

[](#sessionall)

Return the entire `$_SESSION` superglobal.

```
$all = Session::all();
```

### `Session::regenerate()`

[](#sessionregenerate)

Regenerate the session ID. Pass `false` to keep the old session data.

```
Session::regenerate();        // regenerate and delete old session
Session::regenerate(false);   // regenerate but keep old session data
```

### `Session::id()`

[](#sessionid)

Get the current session ID.

```
$id = Session::id();
```

### `Session::name()`

[](#sessionname)

Get the current session name.

```
$name = Session::name();
```

### `Session::end()`

[](#sessionend)

Destroy the session and all its data.

```
Session::end();
```

---

Namespacing
-----------

[](#namespacing)

Sessions are stored under a namespace key (`$for`) within `$_SESSION`. This prevents key collisions when multiple parts of your application share a session.

```
Session::set('id', 42, 'USER');
Session::set('id', 99, 'CART');

Session::get('id', 'USER'); // 42
Session::get('id', 'CART'); // 99
```

The default namespace is `APP`.

---

Full Bootstrap Example
----------------------

[](#full-bootstrap-example)

```
use Laika\Session\SessionManager;
use Laika\Session\Session;

// 1. Configure driver
$pdo = new PDO('mysql:host=127.0.0.1;dbname=myapp', 'user', 'pass');
SessionManager::config($pdo);

// 2. Customise options (optional)
SessionManager::setOptions(['name' => 'MY_APP', 'gc_maxlifetime' => 7200]);
SessionManager::setCookies(['domain' => '.example.com']);

// 3. Use the Session facade anywhere
Session::set('user_id', 1);

if (Session::has('user_id')) {
    $id = Session::get('user_id');
    Session::regenerate(); // rotate session ID on privilege change
}

// On logout
Session::end();
```

---

License
-------

[](#license)

MIT — see [LICENSE](LICENSE) for full terms.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance89

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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 ~39 days

Total

5

Last Release

54d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7272cd554fe8bf859b8450494f244927ee210cd95d516325fda55d33c74e5886?d=identicon)[riyadhtayf](/maintainers/riyadhtayf)

---

Top Contributors

[![laikait](https://avatars.githubusercontent.com/u/100719384?v=4)](https://github.com/laikait "laikait (54 commits)")

---

Tags

phppdoredishandlermemcachedsessionsingleton

### Embed Badge

![Health badge](/badges/laikait-laika-session/health.svg)

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

###  Alternatives

[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

114542.8k6](/packages/apix-cache)[craftsys/laravel-redis-session-enhanced

Enhanced redis driver for sessions in Laravel

106.6k](/packages/craftsys-laravel-redis-session-enhanced)[eftec/cacheone

A Cache library with minimum dependency

103.5k4](/packages/eftec-cacheone)

PHPackages © 2026

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