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

kumar92varun's avatar

Laravel session helper always returning null for first time on page

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?

0 likes
6 replies
Saikishore's avatar

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.

1 like
kumar92varun's avatar

Hey Kishore, Thanks for your answer.

The first line:-

$session = session('paramount', []);

Is trying to get the value of session key "paramount", and the second argument [] is the default value supplied if the session key "paramount" doesn't exists. []

Anyways I removed the second argument, the first var_dump output changed to NULL instead of array(0) {}

Every time on a page hit the session global helper returning NULL for the key "paramount".

1 like
Snapey's avatar

It implies there is a problem with sessions in general.

Can you login and stay logged in for instance?

Are you using a regular browser to access your page?

1 like
kumar92varun's avatar
kumar92varun
OP
Best Answer
Level 1

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!

1 like
slawkens's avatar

This has to do something with the size of the session/cookie.

Cause for me it works fine until some "limit", after which it returns "null".

After disabling that "EncryptCookies::class" in Kernel.php it started working, but it looks like some kind of limit of the cookie content or something?

Please or to participate in this conversation.