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

Shafiuzzaman's avatar

Session issue in Laravel 5.1

My app is built with Laravel 5.1. I have saved some data in session using the code, session(['first_name' => Auth::user()->first_name]). The value is set when user is logging in. It's working most of the times but sometimes the session value is being null. I don't know why automatically the session value is getting null. I am tired of solving this kind of weird problem. I need help.

Edited: Also the issue is more serious when user is not logged in with remember token active, the Auth::user() is also getting null for that user. And these situations are happening sometimes, not always. Don't know whether it is server problem or not.

0 likes
8 replies
usama.ashraf's avatar

Check the session expiry time in config/session.php. Increase it.

Shafiuzzaman's avatar

Thanks but it's set to 120 minutes and I am facing the problem at ongoing test so session life time is not affecting here. :-)

chintan's avatar

Please make

 
'domain' => null,

in config/session.php

Shafiuzzaman's avatar

@nolros No I am not using database session. Moreover, I think I found an issue, My project structure is something like this,

I have two code base

  1. Front end (base i,e, public_html)
  2. App (Provider directory inside public_html)

The Front end is on base directory which is public_html in server and App is inside another directory which name is Provider and that lies in base, which means full Front end is in base and full App is in Provider directory. Login system is from Front end that means I am sending credentials with query string as encrypted and in App after decryption, putting email and password in a variable and then authenticating that user. Now the problem is when I am logging in a blank session file is being created in Front end session directory with the same name which is being created in app which is in Provider directory. So there is now two different file with the same name in two different code base. So I made a custom_directory to store session in Front end and now no duplicate blank session file is being created in custom_directory. From then on I am not getting session problem till now. Lets hope this solve the problem.

djbryanth's avatar

@Shafiuzzaman I have the same problem, I dont understand how you solve the problem please help me... I have these structure...

Inside de Laravel structure in public I have all of my front end that develop in angular, the laravel session configuration is the same.. but sometimes the session value is being null I don't know when, is random.....

PLEASE HELP

Shafiuzzaman's avatar

@djbryanth are those values in session, you set using session or Auth values. Sometimes auth values get null, and I am finding that issue yet but this can be solved if you keep the remomber token always on that is when auth, besides your credentials you send true as the second parameter in auth. To clarify more please check the auth documentation. If your app structure is like my app then, you can use custom directory to store session of parent app which lies directly in base directory. Hope you got my points :)

Please or to participate in this conversation.