PHPackages                             middlewares/php-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. [HTTP &amp; Networking](/categories/http)
4. /
5. middlewares/php-session

ActiveLibrary[HTTP &amp; Networking](/categories/http)

middlewares/php-session
=======================

Middleware to start php sessions using the request data

v3.2.0(1y ago)1525.8k↓29.7%3[1 issues](https://github.com/middlewares/php-session/issues)3MITPHPPHP ^7.2 || ^8.0CI passing

Since Oct 8Pushed 1y ago1 watchersCompare

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

READMEChangelog (10)Dependencies (8)Versions (16)Used By (3)

middlewares/php-session
=======================

[](#middlewaresphp-session)

[![Latest Version on Packagist](https://camo.githubusercontent.com/eb6db8af7e4ae74fa8231df1f42aca277144940bbe7c587188d83241ea256628/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f7068702d73657373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/php-session)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/php-session/workflows/testing/badge.svg)](https://github.com/middlewares/php-session/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/e2a75509bf7eef92174bc7da7cf964fc54387e36c37b903cfc0a946774cb74b1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f7068702d73657373696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/php-session)

Middleware to start a [php session](http://php.net/manual/en/book.session.php) using the request data and close it after returning the response. Reads and writes session cookies in the PSR-7 request/response.

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

[](#requirements)

- PHP &gt;= 7.2
- A [PSR-7 http message implementation](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

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

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/php-session](https://packagist.org/packages/middlewares/php-session).

```
composer require middlewares/php-session
```

Example
-------

[](#example)

```
Dispatcher::run([
	new Middlewares\PhpSession(),

    function () {
        //Use the global $_SESSION variable to get/set data
        $_SESSION['name'] = 'John';
    }
]);
```

Usage
-----

[](#usage)

This is a middleware to start the native PHP session using the cookies of the server request.

### name

[](#name)

The session name. If it's not provided, use the php's default name (PHPSESSID). More info [session\_name](https://www.php.net/manual/en/function.session-name.php)

```
// Start the session with other name
$session = (new Middlewares\PhpSession())->name('user_session');
```

### id

[](#id)

This option set a session id. If it's not provided, use the request's cookies to get it.

```
// Start the session with a specific session id
$session = (new Middlewares\PhpSession())->id('foo');
```

### options

[](#options)

This allows to set an of options passed to [`session_start()`](http://php.net/session_start)

```
// Start the session with a specific session id
$session = (new Middlewares\PhpSession())->options([
    'cookie_lifetime' => 86400
]);
```

### regenerateId

[](#regenerateid)

This option regenerates the id after a specific time interval. The latest regeneration time is saved in the key `session-id-expires` but you can change it in the second argument:

```
// Regenerate the session id after 60 seconds
$session = (new Middlewares\PhpSession())->regenerateId(60);

// Regenerate the session id after 60 seconds, storing the expires date in the key 'expiresAt'
$session = (new Middlewares\PhpSession())->regenerateId(60, 'expiresAt');
```

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance44

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor1

Top contributor holds 81.6% 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 ~372 days

Total

15

Last Release

422d ago

Major Versions

v0.6.0 → v1.0.02018-01-27

v1.2.0 → v2.0.02020-12-03

v2.0.0 → v3.0.02021-02-22

PHP version history (3 changes)v0.1.0PHP ^5.6 || ^7.0

v0.6.0PHP ^7.0

v2.0.0PHP ^7.2 || ^8.0

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (62 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (6 commits)")[![compwright](https://avatars.githubusercontent.com/u/138688?v=4)](https://github.com/compwright "compwright (3 commits)")[![lekoala](https://avatars.githubusercontent.com/u/250762?v=4)](https://github.com/lekoala "lekoala (2 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (2 commits)")[![darkalchemy](https://avatars.githubusercontent.com/u/30264374?v=4)](https://github.com/darkalchemy "darkalchemy (1 commits)")

---

Tags

httpmiddlewarepsr-15sessionhttppsr-7middlewareserverpsr-15session

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)[middlewares/http-authentication

Middleware to implement Basic and Digest Http authentication

35302.0k2](/packages/middlewares-http-authentication)[middlewares/client-ip

Middleware to detect the client ip and save it as a request attribute

16629.7k9](/packages/middlewares-client-ip)

PHPackages © 2026

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