DEV Community

Cover image for How to get all user sessions in Laravel from Redis
Kevin Woblick
Kevin Woblick

Posted on • Originally published at blog.kovah.de

3 2

How to get all user sessions in Laravel from Redis

I wanted to access all session data for all users in Laravel while using the Redis cache driver. If you have all session data, you can inspect the data associated to that session, do some calculations based on averages across all session, or whatever else you want to do.
Unfortunately, Laravel does not offer any way to just get all user sessions at once. I searched the web for solutions, only to find out that users recommend switching to the database to store user sessions. Obviously, not a solution for any production system, and really not a good solution if you consider the mediocre performance of any database compared to Redis.

How to access all session data in Laravel

So, here's how to access all user sessions stored in Redis. I assume that you use Laravel 8 with PHP 8. You may use this with older PHP versions if you replace the function shorthand with a full function.

use \Illuminate\Support\Facades\Redis;

$keys = array_map(fn($key) => str_replace('laravel_database_', '', $key), Redis::keys('*'));
$data = array_map(fn($data) => unserialize(unserialize($data)), Redis::mget($keys));
Enter fullscreen mode Exit fullscreen mode

If you changed your APP_NAME, replace laravel in the laravel_database_ string with your value.

Example: If your application name is MyApp you should use myapp_database_.

It's really just two lines of code, which should get you something like this:

array:2 [▼
  0 => array:3 [▼
    "_token" => "1jMe1...oWSJe"
    "_previous" => array:1 [▶]
    "_flash" => array:2 [▶]
  ]
  1 => array:9 [▼
    "_token" => "z29O...W3lT"
    "url" => []
    "_previous" => array:1 [▶]
    "_flash" => array:2 [▶]
    "login" => []
    "login_web_59ba...989d" => 1
    "password_hash_web" => "$2y$10$6H...bnS"
    "password_hash_sanctum" => "$2y$10$6H...bnS"
  ]
]
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

Let us walk through the two lines to get a better understanding of the code.

Redis has no feature to get all data at once. First, we need to get all keys from Redis. Laravel automatically builds the Redis keys based the application name and other parts. To work with the "real keys" later, we have to remove this Laravel-internal string from the key. We do this in the str_replace part, where we remove laravel_database_ from all returned keys. The $keys array then holds the keys for all user sessions.

$keys = array_map(fn($key) => str_replace('laravel_database_', '', $key), Redis::keys('laravel_database_*'));
Enter fullscreen mode Exit fullscreen mode

The next step is to get the data for all those keys. Thankfully, Redis has the MGET command, which lets you get the data for an array of keys. For whatever reason, the data returned is serialized twice. I guess it's one serialization for storing data in Redis and one for actually serializing the session data. Whatever the reason might be, we get the data via Redis::mget($keys) and then loop through all returned datasets and unserialize the string we got from Redis twice.

$data = array_map(fn($data) => unserialize(unserialize($data)), Redis::mget($keys));
Enter fullscreen mode Exit fullscreen mode

The result is an array stored in $data that holds all user sessions as arrays with their corresponding data.


This article was first published on my blog.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay