{"id":27143432,"url":"https://github.com/craftsys/laravel-redis-session-enhanced","last_synced_at":"2025-04-08T08:40:31.965Z","repository":{"id":235979507,"uuid":"791656305","full_name":"craftsys/laravel-redis-session-enhanced","owner":"craftsys","description":"Enhanced Redis driver for Sessions in Laravel, like the Database driver","archived":false,"fork":false,"pushed_at":"2024-04-25T12:50:34.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-26T06:43:24.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/craftsys.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-25T05:53:21.000Z","updated_at":"2024-04-25T12:50:37.000Z","dependencies_parsed_at":"2024-04-25T06:53:18.253Z","dependency_job_id":null,"html_url":"https://github.com/craftsys/laravel-redis-session-enhanced","commit_stats":null,"previous_names":["craftsys/laravel-redis-session-enhanced"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craftsys%2Flaravel-redis-session-enhanced","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craftsys%2Flaravel-redis-session-enhanced/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craftsys%2Flaravel-redis-session-enhanced/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/craftsys%2Flaravel-redis-session-enhanced/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/craftsys","download_url":"https://codeload.github.com/craftsys/laravel-redis-session-enhanced/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247806252,"owners_count":20999306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-08T08:40:31.313Z","updated_at":"2025-04-08T08:40:31.951Z","avatar_url":"https://github.com/craftsys.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Redis Session Enhanced Driver\n\n\u003cp\u003e\n\u003ca href=\"https://packagist.org/packages/craftsys/laravel-redis-session-enhanced\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/craftsys/laravel-redis-session-enhanced\" alt=\"Total Downloads\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/craftsys/laravel-redis-session-enhanced\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/craftsys/laravel-redis-session-enhanced?label=version\" alt=\"Latest Stable Version\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/craftsys/laravel-redis-session-enhanced\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/craftsys/laravel-redis-session-enhanced\" alt=\"License\" /\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/craftsys/laravel-redis-session-enhanced/actions/workflows/test.yml\"\u003e\u003cimg src=\"https://github.com/craftsys/laravel-redis-session-enhanced/actions/workflows/test.yml/badge.svg\" alt=\"Status\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\nThe [Laravel's Database Session Driver](https://laravel.com/docs/session#database) 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\n\n- Track Active Sessions\n- Remove Other Sessions (Logout from other devices)\n- Allow Admins to force logout some/everyone\n- Block Multiple Sessions\n\nBut 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](https://laravel.com/docs/session#redis). The redis driver will store and validate the session automatically but you will loose the above mentioned capabilities for your customers (tracking, logouts etc.).\n\nIf you want to have similar capabilities as the Database Session driver but want to use Redis for session storage, this project is for you.\n\n\n## Table of Contents\n\n-   [Installation](#installation)\n-   [Configuration](#configuration)\n-   [Usage](#usage)\n\n## Installation\n\nThe packages is available on [Packagist](https://packagist.org/packages/craftsys/laravel-redis-session-enhanced) and can be installed via [Composer](https://getcomposer.org/) by executing following command in shell.\n\n```bash\ncomposer require craftsys/laravel-redis-session-enhanced\n```\n\n**prerequisite**\n\n-   php^7.1\n-   laravel^5|^6|^7|^8|^9|^10|^11\n-   redis installed and configured for laravel\n\nThe package is tested for 5.8+,^6.0,^7.0,^8.0,^9.0,^10.0,^11.0 only.\n\n### Laravel 5.5+\n\nIf you're using Laravel 5.5 or above, the package will automatically register the `Craftsys\\LaravelRedisSessionEnhanced\\RedisSessionEnhancedServiceProvider` provider.\n\n### Laravel 5.4 and below\n\nAdd `Craftsys\\LaravelRedisSessionEnhanced\\RedisSessionEnhancedServiceProvider` to the `providers` array in your `config/app.php`:\n\n```php\n'providers' =\u003e [\n     // Other service providers...\n     Craftsys\\LaravelRedisSessionEnhanced\\RedisSessionEnhancedServiceProvider::class,\n],\n```\n\n## Configuration\n\nThe package usage your existing configuration files and requires following modification in configs and env file.\n\n\n1. Add a new connection named `session` in your `config/database.php` redis configuration\n```php\n[\n  'redis' =\u003e [\n     // ... existing configuration\n     // Add new connection for session\n     'session' =\u003e [\n        'host' =\u003e env('REDIS_HOST', '127.0.0.1'),\n        'password' =\u003e env('REDIS_PASSWORD', null),\n        'port' =\u003e env('REDIS_PORT', 6379),\n        // new DB, only for sessions for quick access and cleanup, change the value if 2 is already taken\n        'database' =\u003e env('REDIS_CACHE_DB', 2),\n    ],\n  ]\n];\n```\n\n2. Update the .env file with the session driver and connection\n\n```sh\nSESSION_DRIVER=redis-session\nSESSION_CONNECTION=session\n```\n\nIf you have cached your config file, you might want to run `php artisan config:clear` and `php artisan config:cache` to revalidate the cache.\n\n## Usage\n\nOnce you configure, the session data will be automatically stored in Redis cache with automatic validation. The stored session data has following properties in the cache.\n\n```ts\n{\n    \"id\": string, // session id\n    \"user_id\": number|string, // user id\n    \"ip_address\": null|string, // request ip address\n    \"user_agent\": string, // request user agent\n    \"last_activty\": number, // user's last request timestamp\n    \"payload\": string, // serialized/encrypted session data,\n}\n```\n\nIf you want to get the underlying handler of the session (`RedisSessionEnhancerHandler` instance) in your application code, you can use the `Illuminate\\Support\\Facades\\Session::getHandler()`. Along with the required [Session Interface for Custom Drivers by Laravel](https://laravel.com/docs/session#implementing-the-driver), this helper provides `readAll` and `destroyAll` methods to work with stored sessions. This package also includes a helper to work with sessions.\n\n### SessionHelper\n\nTo retrieve the stored session data from the cache, you should use the `Craftsys\\LaravelRedisSessionEnhanced\\SessionHelper` class. This helper class also handles the `SESSION_DRIVER=database` driver so that you can easily switch between database and redis drivers as per your application needs, without changing the application code for sessions.\n\nThe following helper functions are provided:\n\n```php\nuse Craftsys\\LaravelRedisSessionEnhanced\\SessionHelper;\n\n// 1. Show the active/all sessions of a User\nSessionHelper::getForUser($user_id) // get collection of all sessions of a user\nSessionHelper::getForUser($user_id, true) // get collection of all active sessions of a user\n\n// 2. Remove all/other sessions of a user\nSessionHelper::deleteForUserExceptSession($user_id, [request()-\u003esession()-\u003eid]) // delete user's sessions except the current request\nSessionHelper::deleteForUserExceptSession($user_id) // delete all sessions of a user\n\n// 3. Remove All sessions (can be used in a command to flush out all sessions by DevOps)\nSessionHelper::deleteAll() // delete all the sessions stored in database of every\n\n// 4. Check if the application is configured with valid driver (database/redis).\nSessionHelper::isUsingValidDriver() // return true if using SESSION_DRIVER=database or SESSION_DRIVER=redis-session\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraftsys%2Flaravel-redis-session-enhanced","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcraftsys%2Flaravel-redis-session-enhanced","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcraftsys%2Flaravel-redis-session-enhanced/lists"}