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

ActiveLibrary[Framework](/categories/framework)

anax/session
============

Anax Session module, a wrapper around sessions.

v2.0.2(6y ago)119.2k↓90%220MITMakefilePHP ^7.2

Since May 4Pushed 6y ago1 watchersCompare

[ Source](https://github.com/canax/session)[ Packagist](https://packagist.org/packages/anax/session)[ Docs](https://dbwebb.se/anax)[ RSS](/packages/anax-session/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (20)Used By (20)

Anax Session
============

[](#anax-session)

[![Latest Stable Version](https://camo.githubusercontent.com/f964e9c2a2cd5e3e2a14998f69c003c5c60e6e02c18e2a7c4704a8750e33f2f6/68747470733a2f2f706f7365722e707567782e6f72672f616e61782f73657373696f6e2f762f737461626c65)](https://packagist.org/packages/anax/session)[![Join the chat at https://gitter.im/canax/session](https://camo.githubusercontent.com/f4864e391a1055c1aa0eac00657c4cad8b4680eb96c81cfa41af38154407806e/68747470733a2f2f6261646765732e6769747465722e696d2f63616e61782f73657373696f6e2e737667)](https://gitter.im/canax/session?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Build Status](https://camo.githubusercontent.com/da791323a43755d60ef5bf94fa93ca4386bdf5b014a5b7a74cceefcff6666e94/68747470733a2f2f7472617669732d63692e6f72672f63616e61782f73657373696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/canax/session)[![CircleCI](https://camo.githubusercontent.com/52835bbe1f75b30f5e3aaaec63e1c09363c8ccbacfd09287854ff2957772f967/68747470733a2f2f636972636c6563692e636f6d2f67682f63616e61782f73657373696f6e2e7376673f7374796c653d737667)](https://circleci.com/gh/canax/session)

[![Build Status](https://camo.githubusercontent.com/6e2e6dafb35e08bbfa00518f43e600e0903b8947bb982f47e61459e9ff6bf941/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f73657373696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/session/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/31d512ad6cd12c0e41dc6827d26f483b7e67de33158e4a6388cb62674c617fe4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f73657373696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/session/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/f3a62906a26ee7c49d2a9c7886085b0107ffb50eb72d81d1790915fed01cbefa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63616e61782f73657373696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/canax/session/?branch=master)

[![Maintainability](https://camo.githubusercontent.com/ae08512b045b9290fa7d9431dd83d7973214c3ad99da46cb97c90b0d38eaa3f5/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35363436333633373862346631633134313332662f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/canax/session/maintainability)[![Codacy Badge](https://camo.githubusercontent.com/e209ea2e8d50a62dafae6adc93a44ba8ded3a484e87ffa2475bdccdf6d271ef9/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3465393432656665643366373431646239346330323764326631343564313239)](https://www.codacy.com/app/mosbth/session?utm_source=github.com&utm_medium=referral&utm_content=canax/session&utm_campaign=Badge_Grade)

Anax Session module, for wrapping the session and providing useful helpers related to the session.

The Session module is prepared wo be used for unit testing when run in CLI environment.

Table of content
----------------

[](#table-of-content)

- [Class, interface, trait](#class-interface-trait)
- [Exceptions](#exceptions)
- [Configuration file](#configuration-file)
- [DI service](#di-service)
- [Access as framework service](#access-as-framework-service)
- [Start the session](#start-the-session)
- [Work with session variables](#work-with-session-variables)
- [Session flash messages](#session-flash-messages)
- [Debug the session](#debug-the-session)
- [Destroy the session](#destroy-the-session)

Class, interface, trait
-----------------------

[](#class-interface-trait)

The following classes, interfaces and traits exists.

Class, interface, traitDescription`Anax\Session\Session`Wrapper class for sessions.`Anax\Session\SessionInterface`Interface to implement for classes what want to wrap the session.Exceptions
----------

[](#exceptions)

There are no module specific exceptions.

Configuration file
------------------

[](#configuration-file)

This is a sample configuration file, it is usually stored in `config/session.php`.

```
/**
 * Config-file for sessions.
 */
return [
    // Session name
    "name" => preg_replace("/[^a-z\d]/i", "", __DIR__),
    //"name" => preg_replace("/[^a-z\d]/i", "", ANAX_APP_PATH),
];
```

DI service
----------

[](#di-service)

The session is created as a framework service within `$di`. The following is a sample on how the session service is created.

```
/**
 * Creating the session as a $di service.
 */
return [
    // Services to add to the container.
    "services" => [
        "session" => [
            "active" => defined("ANAX_WITH_SESSION") && ANAX_WITH_SESSION, // true|false
            "shared" => true,
            "callback" => function () {
                $session = new \Anax\Session\Session();

                // Load the configuration files
                $cfg = $this->get("configuration");
                $config = $cfg->load("session");

                // Set session name
                $name = $config["config"]["name"] ?? null;
                if (is_string($name)) {
                    $session->name($name);
                }

                $session->start();

                return $session;
            }
        ],
    ],
];
```

The session can always be active, in the default configuration file this is depending on the PHP define `ANAX_WITH_SESSION` which is usually set in `config/commons.php` which is available from the module `anax/commons`.

The session is always started when `ANAX_WITH_SESSION` is defined and `true`.

This is how the callback works, when it creates the session service.

1. The object is created.
2. The configuration file, usually `config/session.php`, is read.
3. The session is named.
4. The session is created.

The service is lazy loaded and not created until it is used. However, it is always loaded when `"active" => true`.

Access as framework service
---------------------------

[](#access-as-framework-service)

You can access the module as a framework service.

```
# $app style
$app->session->start();

# $di style, two alternatives
$di->get("session")->start();

$session = $di->get("session");
$session->start();
```

Start the session
-----------------

[](#start-the-session)

The recommended way, and the default behaviour, is to start the session by defining `ANAX_WITH_SESSION` and set it to `true`. This is usually done in `config/commons.php` which source is available in the module `anax/commons`.

Setting `ANAX_WITH_SESSION` to `false` means that the session is not started by default.

As an alternative, you can start the session anywhere by activating (using) the di service. A good place to do this is in the frontkontroller `index.php`, after you have created `$di` (and `$app`). The session will then be available for all page requests.

The session will not start when running in CLI (for example PHPUnit), however, it will still be active and usable as means of unit testing.

Work with session variables
---------------------------

[](#work-with-session-variables)

There are helpers to use when reading and setting values in the session.

```
# Check if a key is set in the session
$app->session->has($key);

# Get a value from the session, null if it is not set.
$app->session->get($key);

# Get a value from the session or get the default value.
$app->session->get($key, $default);

# Set a value in the session, associated with a key.
$app->session->set($key, $value);

# Delete a key from the session.
$app->session->delete($key);
```

Session flash messages
----------------------

[](#session-flash-messages)

There is feature useful for flash messages. A value is retrieved from the session and is then deleted.

```
# Get the value from the session and then delete its key, default is null.
$app->session->getOnce($key);

# Specify your own default value when key does not exists.
$app->session->getOnce($key, $default);
```

Debug the session
-----------------

[](#debug-the-session)

You can var\_dump the session object and it will print out the content of `$_SESSION`.

```
# Debug the session and review its content through var_dump.
var_dump($app->session);
```

Destroy the session
-------------------

[](#destroy-the-session)

You can destroy the session which might be useful during development.

```
# Destroy the session.
$app->session->destroy();
```

License
-------

[](#license)

This software carries a MIT license. See [LICENSE.txt](LICENSE.txt) for details.

```
 .
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se

```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community24

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 94.9% 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 ~60 days

Recently: every ~150 days

Total

19

Last Release

2252d ago

Major Versions

v1.0.10 → v2.0.0-alpha.12018-08-01

PHP version history (2 changes)v1.0.0PHP &gt;=5.6

v2.0.0-alpha.1PHP ^7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cac244120f54c02a4fb3186b91323f3f0805dd9daefd0babeb4a82a19232faf?d=identicon)[mikael\_roos](/maintainers/mikael_roos)

---

Top Contributors

[![mosbth](https://avatars.githubusercontent.com/u/169550?v=4)](https://github.com/mosbth "mosbth (37 commits)")[![gitter-badger](https://avatars.githubusercontent.com/u/8518239?v=4)](https://github.com/gitter-badger "gitter-badger (1 commits)")[![NiklasAurora](https://avatars.githubusercontent.com/u/11875601?v=4)](https://github.com/NiklasAurora "NiklasAurora (1 commits)")

---

Tags

frameworkmicromvcboilerplateeducation

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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