PHPackages                             mouf/utils.session.optimistic-session-handler - 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. mouf/utils.session.optimistic-session-handler

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

mouf/utils.session.optimistic-session-handler
=============================================

Session handler that releases session lock quickly. Useful for multiple ajax calls on the same page.

v2.0.0(6y ago)63.3k12[1 issues](https://github.com/thecodingmachine/utils.session.optimistic-session-handler/issues)[1 PRs](https://github.com/thecodingmachine/utils.session.optimistic-session-handler/pulls)MITPHPPHP &gt;= 7.2.0CI failing

Since Aug 10Pushed 4y ago8 watchersCompare

[ Source](https://github.com/thecodingmachine/utils.session.optimistic-session-handler)[ Packagist](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)[ Docs](https://github.com/ceckobecko/utils.session.optimistic-session-handler)[ RSS](/packages/mouf-utilssessionoptimistic-session-handler/feed)WikiDiscussions 2.0 Synced today

READMEChangelog (10)Dependencies (3)Versions (14)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/5b0936a3caa1a4cefda21223898b1238f897d0cc9942d2bc924d346a04f1eab9/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f7574696c732e73657373696f6e2e6f7074696d69737469632d73657373696f6e2d68616e646c65722f762f737461626c65)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)[![Latest Unstable Version](https://camo.githubusercontent.com/06e68111a11d40194522ffacb13c3ebe0b594a6b6a3fb51c4edd48daba32f526/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f7574696c732e73657373696f6e2e6f7074696d69737469632d73657373696f6e2d68616e646c65722f762f756e737461626c65)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)[![License](https://camo.githubusercontent.com/e4faeed1a54195d52da78b7ee7e7be96fb550f273474b0d75d6b14091836fb01/68747470733a2f2f706f7365722e707567782e6f72672f6d6f75662f7574696c732e73657373696f6e2e6f7074696d69737469632d73657373696f6e2d68616e646c65722f6c6963656e7365)](https://packagist.org/packages/mouf/utils.session.optimistic-session-handler)[![Build Status](https://camo.githubusercontent.com/51cbcc6fb23b386845b531525d2695e1d3ff15a83bfdc06cff0d55cf94f7814c/68747470733a2f2f7472617669732d63692e6f72672f746865636f64696e676d616368696e652f7574696c732e73657373696f6e2e6f7074696d69737469632d73657373696f6e2d68616e646c65722e7376673f6272616e63683d312e30)](https://travis-ci.org/thecodingmachine/utils.session.optimistic-session-handler)

OptimisticSessionHandler
========================

[](#optimisticsessionhandler)

PHP file-based session handler that **releases session lock quickly**. Useful to speed up multiple Ajax calls on the same page.

Why do we need this session handler?
------------------------------------

[](#why-do-we-need-this-session-handler)

It improves performances in case of projects with multiple and long Ajax requests running. Several requests using the user session can be concurrently executed by the server since the session is not locked for a long time.

By default, PHP writes session files on the disk. When you execute `session_start`, the session file is opened, and a lock is put on the file. If another process tries to perform a `session_start`, the process will wait until the lock on the session file is released. This is a security feature of PHP (2 processes cannot modify the same session at the same time), but this is dreadful for performances, as PHP requests sharing the same session must be run sequentially.

This package offers a way around this problem. It assumes that *everything is going to be alright* (hence the "optimistic" name), and let several processes access the session at the same time. If two processes modify the session at the same time, it will try to merge the 2 results. If it fails to do so, it will throw an exception.

How does it work?
-----------------

[](#how-does-it-work)

This session handler modifies the default behaviour of PHP session handling. Sessions are still written to disk (like PHP does by default). The session is opened and read when you call `session_start()` to fill the global variable `$_SESSION`. But the session is closed immediately after. At the end of your PHP script, the `$_SESSION` is compared with the old session. If the session has been modified in your script, the handler re-opens a session and compare the new session with your changes. The merged session is saved.

*Note:* if you use an alternative session handler (like APC or Memcache), do not use this session handler. It is designed to be used with file based sessions.

\## Using the session handler It's extremely easy to use. Just declare a new instance :

```
$handler = new OptimisticSessionHandler();

```

And save it as your default session handler :

```
session_set_save_handler($handler, true);

```

Then you can start the session as usual

```
session_start(['read_and_close' => true]);

```

Then the `$_SESSION` array is accessible.

You can configure rules for managing conflicts. Just add element to the class parameter $conflictRules. The possible rules are:

- IGNORE =&gt; Don't use the current change.
- OVERWRITE =&gt; Use the current change.
- FAIL =&gt; Throw exception.

So you can just declare a new instance like this:

```
$handler = new OptimisticSessionHandler(array("key_to_override" => OptimisticSessionHandler::OVERRIDE));

```

### Destroying the session

[](#destroying-the-session)

**Warning:** The session can't be destroyed by `session_destroy()` (It will throw an error). To destroy the session, you must empty the `$_SESSION` array.

```
$_SESSION = array();

```

If you want more information about this package you can go on [OptimisticSessionHandler: A New Way To Think PHP Sessions](http://www.thecodingmachine.com/optimisticsessionhandler-a-new-way-to-think-php-sessions/)

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~306 days

Total

14

Last Release

2412d ago

Major Versions

v1.1.10 → 2.0.x-dev2019-11-26

PHP version history (2 changes)v1.1.0PHP &gt;=5.3.0

2.0.x-devPHP &gt;= 7.2.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1104771?v=4)[mouf](/maintainers/mouf)[@Mouf](https://github.com/Mouf)

---

Top Contributors

[![xhuberty](https://avatars.githubusercontent.com/u/8350192?v=4)](https://github.com/xhuberty "xhuberty (18 commits)")[![moufmouf](https://avatars.githubusercontent.com/u/1290952?v=4)](https://github.com/moufmouf "moufmouf (15 commits)")[![tzkoshi](https://avatars.githubusercontent.com/u/3149847?v=4)](https://github.com/tzkoshi "tzkoshi (15 commits)")[![nguyenk](https://avatars.githubusercontent.com/u/2227554?v=4)](https://github.com/nguyenk "nguyenk (6 commits)")[![Ngob](https://avatars.githubusercontent.com/u/2749238?v=4)](https://github.com/Ngob "Ngob (1 commits)")[![vaidiep](https://avatars.githubusercontent.com/u/2227695?v=4)](https://github.com/vaidiep "vaidiep (1 commits)")

---

Tags

handlersession

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mouf-utilssessionoptimistic-session-handler/health.svg)

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

###  Alternatives

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514139.2M691](/packages/symfony-lock)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k53](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)[illuminate/broadcasting

The Illuminate Broadcasting package.

7127.2M209](/packages/illuminate-broadcasting)[logiscape/mcp-sdk-php

Model Context Protocol SDK for PHP

368116.8k12](/packages/logiscape-mcp-sdk-php)

PHPackages © 2026

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