use global helper
session()->put('blah', 'testvar');
and will get it
session()->get('blah);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have to issue a redirect using the native PHP way, as I'm depending on an external library. When I write values to the session before doing the redirect using header(), they are lost after the request has been redirected.
function someLibraryFunction() {
$request = request();
$request->session()->put('foo', 'bar');
// This does work: echo redirect('/get');
// But this doesn't:
header('Location: /get');
exit;
};
// These are web routes that have the relevant session middleware applied to them.
Route::get('/set', function (Request $request) {
someLibraryFunction();
});
Route::get('/get', function (Request $request) {
print_r($request->session()->all()); // 'foo' is not set here.
});
NOTE: I've verified that session()-getId() is the same before and after the redirect.
Any ideas?
Calling $request->session()->save() before redirecting fixes it. Apparently, actual writing to the session is delayed.
Please or to participate in this conversation.