xp-forge / sessions
Sessions
Installs: 37 818
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.0.0
- xp-forge/web: ^4.0 | ^3.0 | ^2.0 | ^1.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0 | ^9.0 | ^8.0 | ^7.3
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
Example
use web\session\{InFileSystem, ForTesting}; // Instantiate session factory $sessions= new InFileSystem('/tmp'); $sessions= (new ForTesting())->lasting(3600)->named('psessionid'); // Create a new session $session= $sessions->create(); // Open an existing session... if ($session= $sessions->open($sessionId)) { … } // ...or locate session attached to a request if ($session= $sessions->locate($request)) { … } // Basic I/O operations $session->register('key', 'value'); $value= $session->value('key'); $keys= $session->keys(); $session->remove('key'); // Destroy $session->destroy(); // Close session... $session->close(); // ...or close and then transmit session to response. $session->transmit($response);
Ensure you always either call close()
or transmit()
to have the session data synchronized.
Implementations
This library includes the following implementations:
web.session.InFileSystem
- using the local filesystem with serialized dataweb.session.ForTesting
- in-memory sessions, for testing purposes
Other implementations provide solutions for clustering:
- https://github.com/xp-forge/redis-sessions
- https://github.com/xp-forge/mongo-sessions
- https://github.com/xp-forge/cookie-sessions
Secure
The Secure flag is set for all session cookies. If you develop on localhost using http only, you will need to tell the sessions instance as follows:
// This will omit the "Secure" flag from session cookies in dev environment $sessions= new InFileSystem('/tmp'); if ('dev' === $this->environment->profile()) { $sessions->cookies()->insecure(true); }