You are initializing the session in first line. So the first print, it is showing empty array. Remove that first line. No need to initialize with empty array for session.
Correct me if anything wrong.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am using Session via File driver in Laravel 5.5. The issue is that on every page request I check if session key "paramount" is set or not (for first time visit by the visitor). If not, I am setting the session and if it exists, it should not do anything. Here is the code for the same:-
$session = session('paramount', []);
var_dump($session);
if(empty($session))
{
initSession();
}
var_dump($session);
function initSession()
{
session(['paramount.user' => [
'loggedIn' => 0,
'id' => null,
'fullName' => null,
'firstName' => null
]]);
session(['paramount.cart' => [
'products' => [],
'summary' => [
'noOfProducts' => 0,
'productTotal' => 0
]
]]);
}
The output of the above code is:-
array(0) { }
array(2) { ["user"]=> array(4) { ["loggedIn"]=> int(0) ["id"]=> NULL ["fullName"]=> NULL ["firstName"]=> NULL } ["cart"]=> array(2) { ["products"]=> array(0) { } ["summary"]=> array(2) { ["noOfProducts"]=> int(0) ["productTotal"]=> int(0) } } }
This output means that for the first visit (when session wasn't setup) the var_dump shows empty array, and after setting the session for key "paramount", it shows dump as expected.
When I refresh the page, I should expect that the session has been setup and the first time var_dump should show result as:-
array(2) { ["user"]=> array(4) { ["loggedIn"]=> int(0) ["id"]=> NULL ["fullName"]=> NULL ["firstName"]=> NULL } ["cart"]=> array(2) { ["products"]=> array(0) { } ["summary"]=> array(2) { ["noOfProducts"]=> int(0) ["productTotal"]=> int(0) } } }
array(2) { ["user"]=> array(4) { ["loggedIn"]=> int(0) ["id"]=> NULL ["fullName"]=> NULL ["firstName"]=> NULL } ["cart"]=> array(2) { ["products"]=> array(0) { } ["summary"]=> array(2) { ["noOfProducts"]=> int(0) ["productTotal"]=> int(0) } } }
But on every page refresh the first time var_dump of session key "paramount" shows an empty array.
array(0) {}
And second dump as expected:-
array(2) { ["user"]=> array(4) { ["loggedIn"]=> int(0) ["id"]=> NULL ["fullName"]=> NULL ["firstName"]=> NULL } ["cart"]=> array(2) { ["products"]=> array(0) { } ["summary"]=> array(2) { ["noOfProducts"]=> int(0) ["productTotal"]=> int(0) } } }
Don't know why this is happening, can someone help?
I fixed the problem by commenting the "Encrypt Cookies" middleware in /app/Http/Kernel.php:-
// \App\Http\Middleware\EncryptCookies::class,
After commenting the above middleware, here's how my Kernel looks like:-
protected $middlewareGroups = [
'web' => [
//\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
Thanks all for your answers!
Please or to participate in this conversation.