xp-forge/mongo-sessions

v2.1.0 2024-03-24 12:33 UTC

This package is auto-updated.

Last update: 2024-04-24 12:55:10 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

MongoDB-based sessions implementation.

Example

use web\session\InMongoDB;
use com\mongodb\MongoConnection;

$conn= new MongoConnection('mongo://localhost');
$sessions= new InMongoDB($conn->collection('test', 'sessions'));

Session expiry

By default, cleaning up expired sessions is handled by the implementation. For a more performant version, use TTL indexes as follows.

Setup collection

Issue the following in MongoDB shell:

db.sessions.createIndex({"_created": 1}, {expireAfterSeconds: 86400})

Add TTL flag

Pass InMongoDB::USING_TTL as second parameter to the InMongoDB constructor:

$sessions= new InMongoDB($conn->collection('test', 'sessions'), InMongoDB::USING_TTL);

This will use the expireAfterSeconds value as session duration.