fyre / session
A session library.
Installs: 124
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
pkg:composer/fyre/session
Requires
- fyre/config: ^4.0
- fyre/container: ^1.0
- fyre/db: ^6.0
- fyre/filesystem: ^2.0
- fyre/macro: ^1.0
- fyre/path: ^2.0
- fyre/schema: ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^12
README
FyreSession is a free, open-source session library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/session
In PHP:
use Fyre\Session\Session;
Basic Usage
$session = new Session($container, $config);
Default configuration options will be resolved from the "Session" key in the Config.
$optionsis an array containing configuration options.cookieis an array containing session cookie options.nameis a string representing the cookie name, and will default to "FyreSession".expiresis a number representing the cookie lifetime, and will default to 0.domainis a string representing the cookie domain, and will default to "".pathis a string representing the cookie path, and will default to "/".secureis a boolean indicating whether to set a secure cookie, and will default to true.sameSiteis a string representing the cookie same site, and will default to "Lax".
expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.pathis a string representing the session path, and will default to "sessions".handlerclassNamemust be set to\Fyre\Session\Handlers\Database\PostgresSessionHandler.
$container->use(Config::class)->set('Session', $options);
Autoloading
It is recommended to bind the Session to the Container as a singleton.
$container->singleton(Session::class);
Any dependencies will be injected automatically when loading from the Container.
$session = $container->use(Session::class);
Methods
Clear
Clear the session data.
$session->clear();
Consume
Retrieve and delete a value from the session.
$keyis a string representing the session key.
$value = $session->consume($key);
Delete
Delete a value from the session.
$keyis a string representing the session key.
$session->delete($key);
Destroy
Destroy the session.
$session->destroy();
Get
Retrieve a value from the session.
$keyis a string representing the session key.
$value = $session->get($key);
Has
Determine whether a value exists in the session.
$keyis a string representing the session key.
$has = $session->has($key);
ID
Get the session ID.
$id = $session->id();
Is Active
Determine whether the session is active.
$isActive = $session->isActive();
Refresh
Refresh the session ID.
$deleteOldSessionis a boolean indicating whether to delete the old session, and will default to false.
$session->refresh($deleteOldSession);
Set
Set a session value.
$keyis a string representing the session key.$valueis the value to set.
$session->set($key, $value);
Set Flash
Set a session flash value.
$keyis a string representing the session key.$valueis the value to set.
$session->setFlash($key, $value);
Set Temp
Set a session temporary value.
$keyis a string representing the session key.$valueis the value to set.$expireis a number indicating the number of seconds the value will be valid, and will default to 300.
$session->setTemp($key, $value, $expire);
Start
Start the session.
$optionsis an array containing configuration options.
$session->start($options);
Session Handlers
You can load a specific session handler by specifying the Session.handler.className Config option.
Custom session handlers can be created by extending \Fyre\Session\SessionHandler and implementing the SessionHandlerInterface.
Database
The Database session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\DatabaseSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "".connectionKeyis a string representing the Connection key and will default to "default".pathis a string representing the table name, and will default to "sessions".
$container->use(Config::class)->set('Session.handler', $options);
MySQL
The MySQL database session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\Database\MysqlSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "".connectionKeyis a string representing the Connection key and will default to "default".pathis a string representing the table name, and will default to "sessions".
$container->use(Config::class)->set('Session.handler', $options);
Postgres
The Postgres database session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\Database\PostgresSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "".connectionKeyis a string representing the Connection key and will default to "default".pathis a string representing the table name, and will default to "sessions".
$container->use(Config::class)->set('Session.handler', $options);
File
The File session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\FileSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "".pathis a string representing the directory path, and will default to "sessions".
$container->use(Config::class)->set('Session.handler', $options);
Memcached
The Memcached session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\MemcachedSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "session:".hostis a string representing the Memcached host, and will default to "127.0.0.1".portis a number indicating the Memcached port, and will default to 11211.weightis a string representing the server weight, and will default to 1.
$container->use(Config::class)->set('Session.handler', $options);
Redis
The Redis session handler can be loaded using custom configuration.
$optionsis an array containing configuration options.classNamemust be set to\Fyre\Session\Handlers\RedisSessionHandler.expiresis a number representing the maximum lifetime of a session, and will default to thesession.gc_maxlifetimePHP setting.prefixis a string representing the session key prefix, and will default to "session:".hostis a string representing the Redis host, and will default to "127.0.0.1".passwordis a string representing the Redis passwordportis a number indicating the Redis port, and will default to 6379.databaseis a string representing the Redis database.timeoutis a number indicating the connection timeout.persistis a boolean indicating whether to use a persistent connection, and will default to true.tlsis a boolean indicating whether to use a tls connection, and will default to true.sslis an array containing SSL options.keyis a string representing the path to the key file.certis a string representing the path to the certificate file.cais a string representing the path to the certificate authority file.
$container->use(Config::class)->set('Session.handler', $options);