2017 © Pedro Peláez
 

library mongo-session-handler

A PHP session handler backed by MongoDB.

image

altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  • Friday, July 14, 2017
  • by mudge
  • Repository
  • 3 Watchers
  • 4 Stars
  • 5,999 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 4 % Grown

The README.md

THIS PROJECT IS DEPRECATED

mongo-session-handler is not maintained anymore., (*1)

Mongo Session Handler Build Status

A PHP session handler backed by MongoDB., (*2)

Current version: 2.1.0
Supported PHP versions: 5.4, 5.5, 5.6, 7, (*3)

Note: This package depends on the MongoDB PHP driver extension (mongodb) and its companion PHP library. If you need to use the older, legacy driver (mongo), please see version 1.0., (*4)

Installation

$ composer require altmetric/mongo-session-handler:^2.1

Usage

<?php
use Altmetric\MongoSessionHandler;

$sessions = $mongoClient->someDB->sessions;
$handler = new MongoSessionHandler($sessions);

session_set_save_handler($handler);
session_set_cookie_params(0, '/', '.example.com', false, true);
session_name('my_session_name');
session_start();

API Documentation

public MongoSessionHandler::__construct(MongoDB\Collection $collection[, Psr\Log\LoggerInterface $logger])

$handler = new \Altmetric\MongoSessionHandler($client->db->sessions);
session_set_save_handler($handler);
session_start();

$handler = new \Altmetric\MongoSessionHandler($client->db->sessions, $logger);

Instantiate a new MongoDB session handler with the following arguments:, (*5)

  • $collection: a MongoDB\Collection collection to use for session storage;
  • $logger: an optional PSR-3-compliant logger.

The given $collection will be populated with documents using the following schema:, (*6)

This handler implements the SessionHandlerInterface meaning that it can be registered as a session handler with session_set_save_handler., (*7)

Expiring sessions

If you wish to clean up expired sessions using SessionHandlerInterface::gc, ensure that your session.gc_divisor, session.gc_probability and session.gc_maxlifetime settings are populated accordingly, e.g. the following settings in your php.ini mean that sessions that haven't been updated in over an hour have a 1% chance of being cleaned whenever someone starts a new session:, (*8)

session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 3600

In order to keep session cleaning fast, you should add an index on the last_accessed field of your session $collection, e.g., (*9)

db.sessions.createIndex({last_accessed: 1});

Concurrency

As MongoDB prior to 3.0 does not support document level locking, this session handler operates on a principle of Last Write Wins., (*10)

If a user of a session causes two simultaneous writes then you may end up with the following situation:, (*11)

  1. Window A reads session value of ['foo' => 'bar'];
  2. Window B reads session value of ['foo' => 'bar'];
  3. Window B writes session value of ['foo' => 'baz'];
  4. Window A writes session value of ['foo' => 'quux'].

The session will now contain ['foo' => 'quux'] as it was the last successful write. This may be surprising if you're trying to increment some value in a session as it is not locked during reads and writes:, (*12)

  1. Window A reads session value of ['count' => 0];
  2. Window B reads session value of ['count' => 0];
  3. Window B writes session value of ['count' => 1];
  4. Window A writes session value of ['count' => 1].

Acknowledgements

License

Copyright © 2015-2017 Altmetric LLP, (*13)

Distributed under the MIT License., (*14)

The Versions

14/07 2017

dev-master

9999999-dev https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/07 2017

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

v2.1.0

2.1.0.0 https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/06 2017

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires

19/09 2016

v2.0.0

2.0.0.0 https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2015

v1.0.0

1.0.0.0 https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires

15/03 2015

v0.1.0

0.1.0.0 https://github.com/altmetric/mongo-session-handler

A PHP session handler backed by MongoDB.

  Sources   Download

MIT

The Requires

  • php >= 5.4.0

 

The Development Requires