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

wbali's avatar
Level 1

Get session by session ID

Hello fellow developers!

Is there a way I can fetch a session by its id? I'm making a stateful API and when the user logs in via a post request I somehow want to get the session id (or the session cookie). It doesn't seem that laravel would return it automatically. Is this possible?

Thanks, wbali

0 likes
5 replies
usama.ashraf's avatar

@wbali if you're looking for the user's session after they login, then just use this any where in your code:

auth()->user()

If you want to get some other session value by its key do:

Session::get('the_key')
bestmomo's avatar

Hello,

Try this :

\Crypt::decrypt(\Request::cookie(config('session.cookie')))
mjlovefl's avatar

Or of course no need to decrypt if you are not using cookie encryption as set in config/sessions[ 'encrypt' => false ]

What did take me a minute to figure out, was that at least for Redis as a store, the session data is double serialized.

Using a web route to test this, you can get to the data like:

Route::get('/session', function () {

    echo "<pre>";
    $cookie = Request::cookie( 'laravel_session' );
    $sess = Redis::get('laravel:' . $cookie);
    print_r($sess);

    echo "\n\n\n\n=======================\n\n\n\n";

    $sessdata = unserialize( unserialize( $sess ) );
    print_r( $sessdata );

});

Of course if you changed your prefix you will need to change the 'Redis::get' key.

robbydooo's avatar

I just needed the ID rather than retrieving the session itself.

The above didn't work for me in 5.5 but i managed to get it via:

collect(session()->getDrivers())->first()->getId();
1 like

Please or to participate in this conversation.