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

shanely's avatar

How will you handle session when it expires

Hi, can I ask how will you handle your session if it expires I am using artisan auth, the problem is that when it expires the User object will get error I cannot access anymore the firstname and lastname which I display on top of menu. I want to show message box to let the user sign in again. how to do this ?

Thank you in advance.

0 likes
7 replies
SaeedPrez's avatar
@if(auth()->check())
    Hello {{ auth()->user()->name }} | <a href="{{ route('auth.logout') }}">Logout</a>
@else
    <a href="{{ route('auth.login') }}">Login</a>
@endif
shanely's avatar

@SaeedPrez ,

Is that possible to let the user inform first to show message or alert saying "You sesssion has expired you must logged in again" then click ok redirect to Login page ?

Thank you in advance.

Snapey's avatar

the client cannot know that the session has expired on the server.

There is a package called caffeine that pings the server in the background to keep the session going however this can needlessly tie up server resources.

SaeedPrez's avatar

@shanely

I guess you could create a JavaScript timer that would warn the user and after X minutes log the user out and redirect to login page.

1 like
Snapey's avatar

@SaeedPrez, the problem with this is when the user for some reason has more than one tab open. One tab times out and closes the session. The other window that the user is working in is then timed out for apparently no reason.

SaeedPrez's avatar

@snapey indeed it's not a good solution, but it could be improved... maybe keep a timestamp of the users last "real" server request and the JS would send an ajax request to the server and check that timestamp before logging the user out.

shanely's avatar

Hi Guys,

Have you experience this already ? this is my problem I always get error.

Please or to participate in this conversation.