Download the PHP package kattsoftware/firesessions without Composer
On this page you can find all versions of the php package kattsoftware/firesessions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kattsoftware/firesessions
More information about kattsoftware/firesessions
Files in kattsoftware/firesessions
Package firesessions
Short Description PHP Sessions management library
License MIT
Informations about the package firesessions
Introduction
FireSessions is a PHP library for managing your project sessions. It lets you store the session data on your local disk, on a Memcached server, or a Redis one; the library can manage 3 types of session variables: user data, flash data and temp data. This library, through all its drivers, supports locking for opened sessions.
Installation
You can install FireSessions by using composer:
Configuration
To start using this library, you must set it up, e.g.:
As you can see, the settings are an associative array; they may differ from driver to driver. Below is a list of settings which can be set regardless of the used driver:
Setting | Options | Default value | Description |
---|---|---|---|
driver |
string | Session::FILES_DRIVER |
The session driver to be used; can be any of these constants: Session::FILES_DRIVER , Session::MEMCACHED_DRIVER , Session::REDIS_DRIVER or the name of any custom created driver (see below). |
cookie_name |
string (A-Z, a-z, _ or - are accepted) | fs_session | The name of the cookie that will be send to user's browser. |
expiration |
time, in seconds (int) | 7200 (2 hours) | The number of seconds your session should live, before it expires. Using 0 (zero) will make the session to last until browser is closed. |
match_ip |
true /false (bool) |
false |
Whether to check for user's IP address to be the same as the one when the session was created. There may be some scenarios where enabling this may lead to issues (e.g. a mobile network losing the signal and reconnecting, getting a different IP address). |
regenerate_time |
time, in seconds (int) | 300 | Specifies how many seconds it will take to regenerate the session ID, replacing the current one with another generated. Setting this configuration to 0 will disable the regeneration of session ID (however, disabling this will increase the chances of possible session fixation attacks). |
destroy_on_regenerate |
true /false (bool) |
false |
If session ID regeneration is enabled, this tells whether to delete the old session data on session ID regeneration step or to leave it for deletion by PHP's garbage collector. |
Files driver
The files driver will save your session data files on the server's disk. The only setting left here to be set is save_path
.
save_path
is an absolute full path of a writable directory, where PHP process can put its session files. If it's not set, FileSessions will try to use the session_save_path()
function return value.
Memcached driver
This driver will save all your session data on a Memcached server instance.
Here, save_path
is a comma-delimited list of server:port
pairs:
You may also add a third parameter (:weight
), which specifies the priority of a Memcached server:
Please keep in mind that this may lead to performance gains or loses; it may depend from environment to environment. If you are unsure, do not set the servers' weights.
By default, the Memcached driver will retrieve the local pool servers (Memcached::getServerList()
) available. To disable this, you can use the fetch_pool_servers
setting:
Redis driver
The Redis driver will save your session data on a Redis server instance.
For this driver, the save_path
setting is a comma-delimited set of connecting parameters names and their values (param=value
):
Usage
FireSession implements 3 types of session variables: user data, flash data and temp data.
User data
User data represents the collection of variables you set during a whole session lifetime. You can use them to store the ID of logged user, the privileges for a specific user, etc. Simply put, they are the classic $_SESSION
variables.
To set user data variable, you can use the following call:
However, the recommendation here is to use the $session
variables (the Session
instance), so you will have consistency all over your project.
To get user data variable, you can use the following call:
Once again, the recommendation is to use the Session
instance. :)
If the requested variable doesn't exists, userdata()
will return null
.
If you need all user data, you can ask for the whole associative array of user data:
To remove user data, you can use the following call:
Flash data
Flash data are the same as user data, except their lifetime is the current and the next HTTP request. After that, they will be deleted.
To set flash data, you can use the following call:
To get flash data value, you can use the following call:
If, after being redirected to the next request, you want to keep flash data for another request, you can use the following call:
To remove flash data, you can use the following call:
Temp data
Temp data are similar to flash data, except they live for a giving number of seconds, instead of current and next request.
To set temp data, you can use the following call:
To get temp data value, you can use the following call:
To remove temp data, you can ue the following call:
You can also check if certain variables exist:
If you want to destroy the entire session (which means all types of variables), you can use the following call:
Using the SessionFactory
If, for any reasons, you will have different places in your project where you will need this library, you will need the Session
instance more than one time. In this case, new Session($config)
won't work, because the lib was previously created.
In this case, you can use the SessionFactory
in all places, so you will get always the first instance:
Defining your custom drivers
You may create your own session drivers as well! In this case, you will use the FireSessions\DriversFactory
factory.
To do that, you will need to build a class extending the FireSessions\BaseSessionDriver
class (this, by its nature, implements SessionHandlerInterface
). Besides implementing the SessionHandlerInterface
's methods, you will have to implement the additional acquireLock
and releaseLock
abstract methods as well.
Don't forget that your custom driver should receive the $config
array as well.
After that, please call the parent constructor like this:
Once you are there, you can make use of the already available methods, such as BaseSessionDriver::destroyCookie()
and BaseSessionDriver::getIp()
.
To make your custom driver available for being used, use the following call before instantiating the Session
client:
If you are providing the class name as the second parameter (and not an actual instance of the custom driver), then the factory will try to create an instance by providing $config
as parameter.
Now you are ready to use your custom driver!
Acknowledgments
This library closely follows the implementation and the API of the CodeIgniter 3 Session library. Big thanks go to their members and contributors!
License
The library is licensed under the MIT License (MIT). See the LICENSE
file for more information.
Copyright (c) 2017 KattSoftware dev team.