Documentation

MongoDbSessionHandler extends AbstractSessionHandler
in package

Session handler using the mongodb/mongodb package and MongoDB driver extension.

Tags
author

Markus Bachmann markus.bachmann@bachi.biz

see
https://packagist.org/packages/mongodb/mongodb
see
https://php.net/mongodb

Table of Contents

$collection  : MongoCollection
$igbinaryEmptyData  : mixed
$mongo  : mixed
$newSessionId  : mixed
$options  : array<string|int, mixed>
$prefetchData  : mixed
$prefetchId  : mixed
$sessionName  : mixed
__construct()  : mixed
Constructor.
close()  : mixed
{@inheritdoc}
destroy()  : bool
gc()  : mixed
{@inheritdoc}
open()  : bool
read()  : string
updateTimestamp()  : mixed
{@inheritdoc}
validateId()  : bool
Checks if a session identifier already exists or not.
write()  : bool
doDestroy()  : bool
doRead()  : string
doWrite()  : bool
getMongo()  : Mongo|MongoClient|Client
Return a Mongo instance.
createDateTime()  : MongoDate|UTCDateTime
Create a date object using the class appropriate for the current mongo connection.
getCollection()  : MongoCollection
Return a "MongoCollection" instance.

Properties

Methods

__construct()

Constructor.

public __construct(Client $mongo, array<string|int, mixed> $options) : mixed

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: https://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 : Client

A MongoDB\Client instance

$options : array<string|int, mixed>

An associative array of field options

Tags
throws
InvalidArgumentException

When MongoClient or Mongo instance not provided

throws
InvalidArgumentException

When "database" or "collection" not provided

Return values
mixed

destroy()

public destroy(mixed $sessionId) : bool
Parameters
$sessionId : mixed
Return values
bool

gc()

{@inheritdoc}

public gc(mixed $maxlifetime) : mixed
Parameters
$maxlifetime : mixed
Return values
mixed

open()

public open(mixed $savePath, mixed $sessionName) : bool
Parameters
$savePath : mixed
$sessionName : mixed
Return values
bool

read()

public read(mixed $sessionId) : string
Parameters
$sessionId : mixed
Return values
string

updateTimestamp()

{@inheritdoc}

public updateTimestamp(mixed $sessionId, mixed $data) : mixed
Parameters
$sessionId : mixed
$data : mixed
Return values
mixed

validateId()

Checks if a session identifier already exists or not.

public validateId(mixed $sessionId) : bool
Parameters
$sessionId : mixed
Return values
bool

write()

public write(mixed $sessionId, mixed $data) : bool
Parameters
$sessionId : mixed
$data : mixed
Return values
bool

doDestroy()

protected doDestroy(mixed $sessionId) : bool
Parameters
$sessionId : mixed
Return values
bool

doRead()

protected doRead(mixed $sessionId) : string
Parameters
$sessionId : mixed
Return values
string

doWrite()

protected doWrite(mixed $sessionId, mixed $data) : bool
Parameters
$sessionId : mixed
$data : mixed
Return values
bool

getMongo()

Return a Mongo instance.

protected getMongo() : Mongo|MongoClient|Client
Return values
Mongo|MongoClient|Client

createDateTime()

Create a date object using the class appropriate for the current mongo connection.

private createDateTime([int $seconds = null ]) : MongoDate|UTCDateTime

Return an instance of a MongoDate or \MongoDB\BSON\UTCDateTime

Parameters
$seconds : int = null

An integer representing UTC seconds since Jan 1 1970. Defaults to now.

Return values
MongoDate|UTCDateTime

getCollection()

Return a "MongoCollection" instance.

private getCollection() : MongoCollection
Return values
MongoCollection

Search results