Check for Session timeout in Laravel
Asked Answered
W

4

25

I was just wondering if anyone knew how to check for session timeout in Laravel.

You can check whether the session has a specific item:

if (Session::has('name'))
{
     $name = Session::get('name');
}

But you can't check whether the session has expired. It would be nice so that I can report back to the user in a more specific way. "Your session has timed out, please start again."

Any thoughts?

Winded answered 4/2, 2013 at 14:30 Comment(0)
D
29

Just use the same logic as the session class itself.

if ((time() - Session::activity()) > (Config::get('session.lifetime') * 60))
{
   // Session expired
}

Place this in your 'before' filter - and it will run on every request.

Dudley answered 4/2, 2013 at 17:27 Comment(10)
wow i've never seen ::activity() before. Where is that documented?Winded
It's not documented - I just went to the session class and looked at all the functions to try and work out an answer to your problem :)Dudley
@Winded Although Taylor (Laravel's creator and lead developer) is looking into this to make it easier to do, this answer is definitely the smartest way to go. You really should accept this answer. [:)].Vinegarroon
Can someone help me with this? I am getting this error: Undefined index: last_activity Cdoe is here: paste.laravel.com/gU5Stalemate
@Stalemate Session::activity() returns session['last_activity'] - that is only set during an active session (null otherwise). I'm still trying to make this work too as you can't use isset() during the before filter without throwing a different exception. Besides, that would defeat the purpose of doing calculations using Session::activity() in the first place - if isset() did work, you could then just check if Session::activity() is set and, if not, the session has probably timed out. I'm overcomplicating things for myself but that's where I'm at so far. :)Audun
@TheShiftExchange Call to undefined method Illuminate\Session\Store::activity() why is that? I use Laravel 4Whaleback
Please share the code. Please do let me know where to place the code. Thanks!Chiton
@giannischristofakis: The activity method appears to be gone from Laravel. An alternate approach is documented on Laravel.io.Washtub
is this possible to use in the blade file just like following: @if((time() - Session::activity()) > (Config::get('session.lifetime') * 60)) ...Steiger
activity() become Session::get('LAST_ACTIVITY')Counterman
H
2

Why not do this?

if (!Session::has('name'))
{
     $sessionTimeout = 1;
}

If a session times out then the name will no longer be set. You can then write some code to respond to $sessionTimeout == 1;

Harmonious answered 4/2, 2013 at 16:5 Comment(1)
this doesn't quite do it as I'm trying to check whether there was an item set but has run out of time, not just whether it's there or not.Winded
C
0

BTW, Specifically if you need the session lifetime,

Use this:

{{ session('lifetime') }}

When describing this,

Use this:

session(['User' => $user, 'lifetime' => 3600]);
Cloak answered 3/12, 2019 at 10:26 Comment(0)
C
0

In Laravel 8 onwards, the best solution seems to be something like this:

if(session()->missing("name") && session()->missing("email")) {
   // Replace with your own logic to handle the missing session       
   throw SessionNotFoundException;
}

...where both name and email are two session keys that should always be present in the session. Checking two keys is more robust and prevents the condition triggering when just one of them may be missing due to a bug.

Cranston answered 9/9, 2023 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.