PHPackages                             rancoud/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. [Testing &amp; Quality](/categories/testing)
4. /
5. rancoud/session

ActiveLibrary[Testing &amp; Quality](/categories/testing)

rancoud/session
===============

Session package

6.0.0(1y ago)313.6k↓100%1[1 PRs](https://github.com/rancoud/Session/pulls)1MITPHPPHP &gt;=8.4.0CI passing

Since Mar 3Pushed 1mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (4)Versions (29)Used By (1)

Session Package
===============

[](#session-package)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/1df754406edbda74054c569923cf17f9908623cf27580cc2eb5071485317884c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f73657373696f6e)](https://camo.githubusercontent.com/1df754406edbda74054c569923cf17f9908623cf27580cc2eb5071485317884c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f73657373696f6e)[![Packagist Version](https://camo.githubusercontent.com/6709810c471fa89d28e8819914e561cc385662726e7b1da173cbcba759f3fa96/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616e636f75642f73657373696f6e)](https://packagist.org/packages/rancoud/session)[![Packagist Downloads](https://camo.githubusercontent.com/6c65518dcdf02e5abccd1d1ff6a465544ed1ccaaf63dd3b35bda1eededa3c197/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616e636f75642f73657373696f6e)](https://packagist.org/packages/rancoud/session)[![Composer dependencies](https://camo.githubusercontent.com/aae95fbaa83bc6a3f4597f3a75da45ea46ec236fc324617f0e5a2f15e07fe750/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e)](https://github.com/rancoud/session/blob/master/composer.json)[![Test workflow](https://camo.githubusercontent.com/3d53145905397a1cec1510ed7590ddbc20a254149fe546b0955d7e7bc4925b40/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72616e636f75642f73657373696f6e2f746573742e796d6c3f6272616e63683d6d6173746572)](https://github.com/rancoud/session/actions/workflows/test.yml)[![Codecov](https://camo.githubusercontent.com/8aad36926b3caec7d0e1f8ed03a41e8084d8a3eee12e8f0adf77dfda3088b51a/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f72616e636f75642f73657373696f6e3f6c6f676f3d636f6465636f76)](https://codecov.io/gh/rancoud/session)

Session.

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

[](#installation)

```
composer require rancoud/session
```

Informations
------------

[](#informations)

By default session is in read only (option read\_and\_close = 1).
You can specify it using `Session::setReadWrite()` or `Session::setReadOnly()`

Session::start() is not needed, but:

- Session will automatically start in read only when using `get, has, hasKeyAndValue, getAll`
- Session will automatically start in write mode when using `set, remove, getAndRemove, keepFlash, gc, regenerate`

How to use it?
--------------

[](#how-to-use-it)

Set and get value from $\_SESSION

```
Session::set('key', 'value');
$value = Session::get('key');
```

Use custom options

```
// first way
Session::setOption('name', 'custom_session_name');

// second way
Session::start(['cookie_lifetime' => 1440]);

Session::set('key', 'value');
$value = Session::get('key');
```

With encryption on default php session

```
Session::useDefaultEncryptionDriver('keyForEncryption');
Session::set('key', 'value');
$value = Session::get('key');
```

With File driver

```
Session::useFileDriver();
Session::set('key', 'value');
$value = Session::get('key');
```

With Database driver (you have to install rancoud/database)

```
$conf = new \Rancoud\Database\Configurator([
    'engine'   => 'mysql',
    'host'     => '127.0.0.1',
    'user'     => 'root',
    'password' => '',
    'database' => 'test_database'
]);
$db = new \Rancoud\Database\Database($conf);

// for using a current connection
Session::useCurrentDatabaseDriver($db);

// for creating a new connection
//Session::useNewDatabaseDriver($conf);

Session::set('key', 'value');
$value = Session::get('key');
```

With Redis driver (you have to install predis/predis)

```
$params = [
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
];
$redis = new Predis\Client($params);

// for using a current connection
Session::useCurrentRedisDriver($redis);

// for creating a new connection
//Session::useNewRedisDriver($params);

Session::set('key', 'value');
$value = Session::get('key');
```

With your own driver implementing `SessionHandlerInterface` and/or `SessionUpdateTimestampHandlerInterface`

```
$driver = new MyCustomDriver();
Session::useCustomDriver($driver);
Session::set('key', 'value');
$value = Session::get('key');
```

Session
-------

[](#session)

### Static General Commands

[](#static-general-commands)

- start(\[options: array = \[\]\]): void
- regenerate(): bool
- destroy(): bool
- commit(): void
- rollback(): bool
- unsaved(): bool
- hasStarted(): bool
- getId(): string
- setId(id: string): string
- gc(): void
- setReadOnly(): void
- setReadWrite(): void
- isReadOnly(): bool

### Static Variables $\_SESSION access

[](#static-variables-_session-access)

- set(key: string, value: mixed): void
- get(key: string): mixed
- getAll(): array
- getAndRemove(key: string): mixed
- has(key: string): bool
- hasKeyAndValue(key: string, value: mixed): bool
- remove(key: string): void

### Static Variables flash access

[](#static-variables-flash-access)

Flash data are store in a separate variable.
They will dissapear at the end of the script execution or after `commit()` `unsaved()`.
You can use keepFlash for saving it in $\_SESSION.
When flash data is restore, it will be delete in $\_SESSION.

- setFlash(key: string, value: mixed): void
- getFlash(key: string): mixed
- getAllFlash(): array
- hasFlash(key: string): bool
- hasFlashKeyAndValue(key: string, value: mixed): bool
- keepFlash(\[keys: array = \[\]\]): void

### Static Options

[](#static-options)

- setOption(key: string, value): void
- setOptions(options: array): void
- getOption(key: string): mixed

### Static Driver

[](#static-driver)

- getDriver(): \\SessionHandlerInterface

#### Static PHP Session Default Driver

[](#static-php-session-default-driver)

- useDefaultDriver(): void
- useDefaultEncryptionDriver(key: string, \[method: string|null = null\]): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int

#### Static File Driver

[](#static-file-driver)

- useFileDriver(): void
- useFileEncryptionDriver(key: string, \[method: string|null = null\]): void
- setPrefixForFile(prefix: string): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int

#### Static Database Driver

[](#static-database-driver)

- useNewDatabaseDriver(configuration: \\Rancoud\\Database\\Configurator|array): void
- useCurrentDatabaseDriver(databaseInstance: \\Rancoud\\Database\\Database): void
- useNewDatabaseEncryptionDriver(configuration: \\Rancoud\\Database\\Configurator|array, key: string, \[method: string = null\]): void
- useCurrentDatabaseEncryptionDriver(databaseInstance: \\Rancoud\\Database\\Database, key: string, \[method: string = null\]): void
- setUserIdForDatabase(userId: int): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int

#### Static Redis Driver

[](#static-redis-driver)

- useNewRedisDriver(configuration: array|string): void
- useCurrentRedisDriver(redisInstance: \\Predis\\Client): void
- useNewRedisEncryptionDriver(configuration: array|string, key: string, \[method: string = null\]): void
- useCurrentRedisEncryptionDriver(redisInstance: \\Predis\\Client, key: string, \[method: string = null\]): void
- setLengthSessionID(length: int): void
- getLengthSessionID(): int

#### Static Custom Driver

[](#static-custom-driver)

- useCustomDriver(customDriver: \\SessionHandlerInterface): void

Session options
---------------

[](#session-options)

List of session options you can change:

- save\_path
- name
- save\_handler
- auto\_start
- gc\_probability
- gc\_divisor
- gc\_maxlifetime
- serialize\_handler
- cookie\_lifetime
- cookie\_path
- cookie\_domain
- cookie\_secure
- cookie\_httponly
- cookie\_samesite
- use\_strict\_mode
- use\_cookies
- use\_only\_cookies
- referer\_check
- cache\_limiter
- cache\_expire
- use\_trans\_sid
- trans\_sid\_tags
- trans\_sid\_hosts
- sid\_length
- sid\_bits\_per\_character
- upload\_progress.enabled
- upload\_progress.cleanup
- upload\_progress.prefix
- upload\_progress.name
- upload\_progress.freq
- upload\_progress.min\_freq
- lazy\_write
- read\_and\_close

Driver Informations
-------------------

[](#driver-informations)

### Default

[](#default)

Use SessionHandler

### File

[](#file)

Extends SessionHandler

### Database

[](#database)

You need to install

```
composer require rancoud/database
```

```
CREATE TABLE IF NOT EXISTS `sessions` (
  `id` varchar(128) NOT NULL,
  `id_user` int(10) unsigned DEFAULT NULL,
  `last_access` datetime NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

### Redis

[](#redis)

You need to install

```
composer require predis/predis
```

How to Dev
----------

[](#how-to-dev)

`docker compose build && docker compose run lib composer ci` for launching tests

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance71

Regular maintenance activity

Popularity29

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 72.4% 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 ~108 days

Recently: every ~40 days

Total

25

Last Release

387d ago

Major Versions

1.1.6 → 2.0.02020-07-10

2.0.0 → 3.0.02020-07-25

3.0.1 → 4.0.02020-12-09

4.0.1 → 5.0.02021-04-21

5.2.1 → 6.0.02025-04-20

PHP version history (3 changes)1.1.1PHP &gt;=7.2.0

2.0.0PHP &gt;=7.4.0

6.0.0PHP &gt;=8.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/829a536bd4f71cadcd0266e272ccaf413e3fc9f2937248c9a2317ef0bf2d25ee?d=identicon)[rancoud](/maintainers/rancoud)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (262 commits)")[![rancoud](https://avatars.githubusercontent.com/u/1884186?v=4)](https://github.com/rancoud "rancoud (100 commits)")

---

Tags

composercoveragedatabaseencryptionpackagistphpphp7php8phpunitredissessionsessionhandlerinterface

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

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