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

gibek2's avatar

Get session timeleft

Hi, I would to make session counter for the user so I need actual session timeleft can I got this in Laravel 9 with breeze login system?

I'am changing in .env default time or maybe I must write new function for displaying session time?

0 likes
12 replies
Snapey's avatar
Snapey
Best Answer
Level 122

@Sinnbeck Puzzled by the naive nature of that SO answer. The session will always be at full duration on every server request? You cannot ask the server how much time is left because the session period is reset on every request?

The only thing near a solution is to time it locally in the browser in javascript, but then this is also flawed when the user has more than one tab open.

Agreed?

Sinnbeck's avatar

@Snapey Good point. Didnt think of new tabs. The first tab would show a wrong duration, as the second just refreshed the session :) So the answer is probably "It isnt possible in a meaningful way"

gibek2's avatar

@Sinnbeck I came up with the idea to solve it this way, when the user is authorized, a cookie is set with the value = current date and time + time set in the .env file SESSION_LIFETIME.

public function authorize()
{
    if(!isset($_COOKIE['sessionLeft'])) {
        $dateExpired = date('M d, Y H:i:s', strtotime(' +'.env('SESSION_LIFETIME').' minutes'));
        setcookie('sessionLeft', $dateExpired, time() + (60 * env('SESSION_LIFETIME')), "/");
    }

    return true;
}

When the user logs out, the cookie will be deleted and when @unless (Auth :: check ()) is not true

tomorrow I'm going to write a JS that will count the difference between the current and the saved date in the cookie file and start the counter when the user reloads the page couter starts again from actual date

Sinnbeck's avatar

@gibek2 so if I open a new tab, my session will be refreshed. How will the first tab know of this update and update its Javascript count down?

gibek2's avatar

@Sinnbeck If you open new tab and user is authorized system not set new cookie so js get data from stored cookies looks for my ss

imgur.com/a/9yg5rGQ

I will come back when the js end function I think it's possible

Snapey's avatar

what business problem are you trying to solve?

gibek2's avatar

@Snapey I need to display information about when the logout from the panel occurs and give time to prepere

Snapey's avatar

@gibek2 JUst a countdown on the page then. Why do you need to mess with cookies and session duration?

gibek2's avatar

@Snapey I misunderstood the session issue, earlier I thought that after logging in, the time from logging in is counted and after the set SESSION_TIMELEFT time has elapsed, the user is logged out now I know that it is a session, not user activity, after which he is logged out and going to a new tab refreshes the session, hence the problem you wrote about now I understand it's a difficult combination.

I will probably abandon the implementation of this, if it is necessary, I can solve it so that if JS detects the date exhaustion, e.g. from a cookie file, then it will send information to the rout logout

Thanks for your help and understanding!

Snapey's avatar

@gibek2 ok, as long as you understand that every request to the server by the user's browser, if it hits an endpoint on web.php then the session duration is set back to its full value

Please or to participate in this conversation.