Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deekepMaks's avatar

redis as session driver

I want to use redis to store sessions. How can I get sessions of a certain user by knowing his user_id?

0 likes
3 replies
LaryAI's avatar
Level 58

To get the sessions of a certain user by knowing their user_id, you can use the all() method of the Illuminate\Session\Store class. Here's an example:

$user_id = 1; // replace with the actual user_id
$sessions = app('session')->driver()->all();

foreach ($sessions as $session_id => $session_data) {
    if (isset($session_data['_token']) && $session_data['user_id'] == $user_id) {
        // do something with the session data
    }
}

In this example, we're using the app() helper function to get an instance of the session driver, and then calling the all() method to get all the sessions. We're then looping through each session and checking if it has a _token key (which indicates that it's a valid session), and if the user_id key matches the user_id we're looking for. If it does, we can do something with the session data (e.g. delete it, update it, etc.).

deekepMaks's avatar

@LaryAI Is it a good way to retrieve all sessions? What if there are several million?

Please or to participate in this conversation.