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
@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')
Hello,
Try this :
\Crypt::decrypt(\Request::cookie(config('session.cookie')))
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.
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();
@ROBBYDOOO - you can shorten that to
session()->getId();
// OR
$request->session()->getId();
Please sign in or create an account to participate in this conversation.