\Symfony\Component\HttpFoundation\Session\Storage\HandlerMongoDbSessionHandler

MongoDB session handler.

Summary

Methods
Properties
Constants
__construct()
open()
close()
destroy()
gc()
write()
read()
No public properties found
No constants found
getMongo()
No protected properties found
N/A
getCollection()
$mongo
$collection
$options
N/A

Properties

$mongo

$mongo : \Mongo

Type

\Mongo

$collection

$collection : \MongoCollection

Type

\MongoCollection

$options

$options : array

Type

array

Methods

__construct()

__construct(\Mongo|\MongoClient  $mongo, array  $options) 

Constructor.

List of available options:

  • database: The name of the database [required]
  • collection: The name of the collection [required]
  • id_field: The field name for storing the session id [default: _id]
  • data_field: The field name for storing the session data [default: data]
  • time_field: The field name for storing the timestamp [default: time]
  • expiry_field: The field name for storing the expiry-timestamp [default: expires_at]

It is strongly recommended to put an index on the expiry_field for garbage-collection. Alternatively it's possible to automatically expire the sessions in the database as described below:

A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions automatically. Such an index can for example look like this:

db.<session-collection>.ensureIndex(
    { "<expiry-field>": 1 },
    { "expireAfterSeconds": 0 }
)

More details on: http://docs.mongodb.org/manual/tutorial/expire-data/

If you use such an index, you can drop gc_probability to 0 since no garbage-collection is required.

Parameters

\Mongo|\MongoClient $mongo

A MongoClient or Mongo instance

array $options

An associative array of field options

Throws

\InvalidArgumentException

When MongoClient or Mongo instance not provided

\InvalidArgumentException

When "database" or "collection" not provided

open()

open(string  $savePath, string  $sessionName) : boolean

Re-initializes existing session, or creates a new one.

Parameters

string $savePath

Save path

string $sessionName

Session name, see http://php.net/function.session-name.php

Returns

boolean —

true on success, false on failure

close()

close() : boolean

Closes the current session.

Returns

boolean —

true on success, false on failure

destroy()

destroy(string  $sessionId) : boolean

Destroys a session.

Parameters

string $sessionId

Session ID, see http://php.net/function.session-id

Returns

boolean —

true on success, false on failure

gc()

gc(string|integer  $maxlifetime) : boolean

Cleans up expired sessions (garbage collection).

Parameters

string|integer $maxlifetime

Sessions that have not updated for the last maxlifetime seconds will be removed

Returns

boolean —

true on success, false on failure

write()

write(string  $sessionId, string  $data) : boolean

Writes the session data to the storage.

Care, the session ID passed to write() can be different from the one previously received in read() when the session ID changed due to session_regenerate_id().

Parameters

string $sessionId

Session ID , see http://php.net/function.session-id

string $data

Serialized session data to save

Returns

boolean —

true on success, false on failure

read()

read(string  $sessionId) : string

Reads the session data.

Parameters

string $sessionId

Session ID, see http://php.net/function.session-id

Returns

string —

Same session data as passed in write() or empty string when non-existent or on failure

getMongo()

getMongo() : \Mongo

Return a Mongo instance

Returns

\Mongo

getCollection()

getCollection() : \MongoCollection

Return a "MongoCollection" instance.

Returns

\MongoCollection