PHPackages                             michel/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. michel/session

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

michel/session
==============

PHP Session is a PHP library that optimizes session management, utilizing PHP's native session handling mechanisms for enhanced security and efficiency.

1.0.0(8mo ago)09MITPHPPHP &gt;=7.4

Since Dec 15Pushed 8mo agoCompare

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

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Michel PHP SessionStorage Library
=================================

[](#michel-php-sessionstorage-library)

Introduction
------------

[](#introduction)

The `SessionStorage` is a PHP library that provides a simple implementation of the `SessionStorageInterface`. It allows developers to work with PHP sessions in a convenient way, providing methods to get, set, check, and remove session data. This library requires PHP version 7.4 or higher.

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

[](#installation)

Use [Composer](https://getcomposer.org/)

### Composer Require

[](#composer-require)

```
composer require michel/session

```

Usage
-----

[](#usage)

To start using the `SessionStorage`, you need to create an instance of the `NativeSessionStorage` class. Here's an example of how to do it:

```
use Michel\Session\Storage\NativeSessionStorage;

// Create a new session storage instance

/**
 * Constructor for NativeSessionStorage class.
 *
 * @param array $options Options for session start.
 *                      Possible options:
 *                      - 'name': Session name
 *                      - 'lifetime': Session lifetime
 *                      - 'path': Session save path
 *                      - 'domain': Session domain
 *                      - 'secure': Set to true for secure session
 *                      - 'httponly': Set to true to only allow HTTP access
 * @throws \RuntimeException If session start fails.
 */
$sessionStorage = new NativeSessionStorage();
```

Use Cases
---------

[](#use-cases)

The `SessionStorage` library offers the following methods to interact with PHP sessions:

### 1. Check if a key exists in the session:

[](#1-check-if-a-key-exists-in-the-session)

```
if ($sessionStorage->has('user_id')) {
    // Do something with the user_id
}
```

### 2. Get the value of a session key:

[](#2-get-the-value-of-a-session-key)

```
$userName = $sessionStorage->get('username', 'Guest');
// If the 'username' key exists in the session, $userName will be set to its value,
// otherwise, it will be set to 'Guest'.
```

### 3. Set a value in the session:

[](#3-set-a-value-in-the-session)

```
$userId = 123;
$sessionStorage->put('user_id', $userId);
```

### 4. Remove a key from the session:

[](#4-remove-a-key-from-the-session)

```
$sessionStorage->remove('user_id');
```

### 5. Get all session data as an array:

[](#5-get-all-session-data-as-an-array)

```
$allData = $sessionStorage->all();
```

Interface Implementation
------------------------

[](#interface-implementation)

The NativeSessionStorage class implements the SessionStorageInterface, which extends the ArrayAccess interface. As a result, any class implementing the SessionStorageInterface should also implement the methods defined in the ArrayAccess interface. Here's how you can implement the SessionStorageInterface in a custom class:

```
use Michel\Session\Storage\SessionStorageInterface;

class MyCustomSessionStorage implements SessionStorageInterface
{
    private array $storage;

    public function __construct()
    {
        // Initialize your custom storage mechanism here
        // For example, you could use a database, Redis, or any other storage solution.
        // In this example, we will use an array as a simple custom storage mechanism.
        $this->storage = [];
    }

    public function get(string $key, $default = null)
    {
        return $this->storage[$key] ?? $default;
    }

    public function put(string $key, $value = null): void
    {
        $this->storage[$key] = $value;
    }

    public function all(): array
    {
        return $this->storage;
    }

    public function has(string $key): bool
    {
        return isset($this->storage[$key]);
    }

    public function remove(string $key): void
    {
        unset($this->storage[$key]);
    }

    // Implementing ArrayAccess methods

    public function offsetExists($offset): bool
    {
        return isset($this->storage[$offset]);
    }

    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    public function offsetSet($offset, $value): void
    {
        $this->put($offset, $value);
    }

    public function offsetUnset($offset): void
    {
        $this->remove($offset);
    }
}
```

In this example, we create a custom session storage class `MyCustomSessionStorage`, which implements the `SessionStorageInterface`. It uses a simple array to store session data, but you can replace this with any custom storage mechanism like a database, Redis, etc., depending on your specific use case.

Conclusion
----------

[](#conclusion)

The `SessionStorage` library simplifies working with PHP sessions by providing a clean and easy-to-use interface. It is well-suited for applications that need to manage session data efficiently.

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to open issues or submit pull requests to help improve the library.

License
-------

[](#license)

This library is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

28

—

LowBetter than 51% of packages

Maintenance61

Regular maintenance activity

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

245d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/909a078010ad44ff35146af8288451a3b6fd26f81cb198cbea776a92553c9b8a?d=identicon)[F.Michel](/maintainers/F.Michel)

---

Top Contributors

[![michelphp](https://avatars.githubusercontent.com/u/26349908?v=4)](https://github.com/michelphp "michelphp (1 commits)")

### Embed Badge

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

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

PHPackages © 2026

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