24

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?

Bogdan
  • 43,166
  • 12
  • 128
  • 129
mattl
  • 2,082
  • 3
  • 17
  • 24

3 Answers3

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.

Laurence
  • 58,936
  • 21
  • 171
  • 212
  • 4
    wow i've never seen ::activity() before. Where is that documented? – mattl Feb 04 '13 at 20:28
  • 16
    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 :) – Laurence Feb 04 '13 at 23:55
  • @mattl 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. [:)]. – jonathanmarvens Feb 05 '13 at 04:24
  • Can someone help me with this? I am getting this error: Undefined index: last_activity Cdoe is here: http://paste.laravel.com/gU5 – OneSneakyMofo Feb 07 '13 at 02:03
  • @OneSneakyMofo 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. :) –  Feb 13 '13 at 09:38
  • 3
    @TheShiftExchange `Call to undefined method Illuminate\Session\Store::activity()` why is that? I use *Laravel 4* – giannis christofakis Sep 13 '13 at 09:51
  • Please share the code. Please do let me know where to place the code. Thanks! – 2plus Aug 05 '14 at 14:37
  • 4
    @giannischristofakis: The `activity` method appears to be gone from Laravel. [An alternate approach is documented on Laravel.io](http://laravel.io/forum/04-27-2014-how-to-expire-session-data?page=1#reply-13586). – bishop Sep 05 '14 at 15:26
  • is this possible to use in the blade file just like following: `@if((time() - Session::activity()) > (Config::get('session.lifetime') * 60)) ...` – utdev Feb 22 '17 at 16:19
  • `activity()` become `Session::get('LAST_ACTIVITY')` – Nurkartiko Aug 06 '21 at 04:14
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;

thestepafter
  • 598
  • 1
  • 4
  • 20
  • 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. – mattl Feb 04 '13 at 20:25
0

BTW, Specifically if you need the session lifetime,

Use this:

{{ session('lifetime') }}

When describing this,

Use this:

session(['User' => $user, 'lifetime' => 3600]);
Sahin S.
  • 41
  • 4