xp-forge / mongo-sessions
MongoDB Sessions
v3.0.0
2025-01-25 14:49 UTC
Requires
- php: >=7.4.0
- xp-forge/mongodb: ^3.0 | ^2.2
- xp-forge/sessions: ^3.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
MongoDB-based sessions implementation.
Example
use web\session\InMongoDB; use com\mongodb\MongoConnection; $conn= new MongoConnection('mongodb://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.