The Laravel's Database Session Driver manages sessions in the Database which associates following attributes (along with the payload) with every session update: user_id
, ip_address
, user_agent
, and last_activty
. These attributes can be accessed and modified to provide following capabilities to your customers
- Track Active Sessions
- Remove Other Sessions (Logout from other devices)
- Allow Admins to force logout some/everyone
- Block Multiple Sessions
But with the Database driver, every request to your application will do a Database update to sessions table to track the latest session information, specifically, the last_activty
attribute. This database update is required to validate unauthenticated requests if the session becomes inactive for the configured SESSION_LIFETIME
. These session updates on every requests are fast and should not have much performance impact on your request time. But, if you ARE facing performance issues and want to store the session in the redis cache, you can go the Laravel's Redis Session Driver. The redis driver will store and validate the session automatically but you will loose the above mentioned capabilities for your customers (tracking, logouts etc.).
If you want to have similar capabilities as the Database Session driver but want to use Redis for session storage, this project is for you.
The packages is available on Packagist and can be installed via Composer by executing following command in shell.
composer require craftsys/laravel-redis-session-enhanced
prerequisite
- php^7.1
- laravel^5|^6|^7|^8|^9|^10|^11
- redis installed and configured for laravel
The package is tested for 5.8+,^6.0,^7.0,^8.0,^9.0,^10.0,^11.0 only.
If you're using Laravel 5.5 or above, the package will automatically register the Craftsys\LaravelRedisSessionEnhanced\RedisSessionEnhancedServiceProvider
provider.
Add Craftsys\LaravelRedisSessionEnhanced\RedisSessionEnhancedServiceProvider
to the providers
array in your config/app.php
:
'providers' => [
// Other service providers...
Craftsys\LaravelRedisSessionEnhanced\RedisSessionEnhancedServiceProvider::class,
],
- Add a new connection named
session
in yourconfig/database.php
redis configuration
[
'redis' => [
// ... existing configuration
// Add new connection for session
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
// new DB, only for sessions for quick access and cleanup, change the value if 2 is already taken
'database' => env('REDIS_CACHE_DB', 2),
],
]
];
- Update the .env file with the session driver and connection
SESSION_DRIVER=redis-session
SESSION_CONNECTION=session