PHPackages                             nixphp/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. [Framework](/categories/framework)
4. /
5. nixphp/session

ActiveNixphp-plugin[Framework](/categories/framework)

nixphp/session
==============

NixPHP Session Plugin for storing data across requests.

v0.1.1(2mo ago)0138↓50%1MITPHPPHP &gt;=8.3CI passing

Since Nov 30Pushed 2mo ago1 watchersCompare

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

READMEChangelog (1)Dependencies (3)Versions (6)Used By (1)

[![Logo](https://camo.githubusercontent.com/075b2860e9651b98b8c190a8296595c54cff6900890d9e494f31131145e98a6f/68747470733a2f2f6e69787068702e6769746875622e696f2f646f63732f6173736574732f6e69787068702d6c6f676f2d736d616c6c2d7371756172652e706e67)](https://camo.githubusercontent.com/075b2860e9651b98b8c190a8296595c54cff6900890d9e494f31131145e98a6f/68747470733a2f2f6e69787068702e6769746875622e696f2f646f63732f6173736574732f6e69787068702d6c6f676f2d736d616c6c2d7371756172652e706e67)

[![NixPHP Session Plugin](https://github.com/nixphp/session/actions/workflows/php.yml/badge.svg)](https://github.com/nixphp/session/actions/workflows/php.yml)

[← Back to NixPHP](https://github.com/nixphp/framework)

---

nixphp/session
==============

[](#nixphpsession)

> **Simple session management for NixPHP, with flash message support built-in.**

This plugin adds a lightweight session layer to your NixPHP app, starts sessions safely in HTTP requests, and exposes helpers so you can store data (including flash messages) without worrying about headers or manual initialization.

> 🧩 Part of the official NixPHP plugin collection. Install it when you need session persistence, and nothing else.

---

📦 Features
----------

[](#-features)

- Starts PHP sessions automatically (skipping CLI)
- Safeguards cookie params (secure/HttpOnly/SameSite) and regenerates IDs on demand
- Flash message helpers (`flash`, `getFlash`)
- `session()` helper bound in the container
- Optional database-backed storage when `nixphp/database` is installed
- Registers the migration path so `vendor/bin/nix migrate up/down` can create the sessions table (requires `nixphp/cli`)

---

📥 Installation
--------------

[](#-installation)

```
composer require nixphp/session
```

Once installed, the plugin is autoloaded and ready to use. If you install `nixphp/database` too, it can store sessions in your database table instead of native PHP files.

---

Usage
-----

[](#usage)

### Accessing the session

[](#accessing-the-session)

Use the global `session()` helper to access the session storage:

```
session()->set('user_id', 42);

$userId = session()->get('user_id');
```

To remove a key:

```
session()->forget('user_id');
```

---

### Flash messages

[](#flash-messages)

Use flash messages to store data for the *next* request only (e.g. after a redirect):

```
session()->flash('success', 'Profile updated.');
```

In the next request, access it using:

```
