4

How do I check if the session has expired in laravel 5.4.

If the session has expired I want to show a message.

Olipol
  • 315
  • 1
  • 9
  • 19
  • 1
    Possible duplicate of [Check for Session timeout in Laravel](https://stackoverflow.com/questions/14688853/check-for-session-timeout-in-laravel) – Ken Jun 23 '17 at 14:05
  • Where will you check for the session? is it in a controller fucntion? or from a view? – Onix Jun 23 '17 at 14:13
  • Auth::check() and Auth::guest() DO NOT work to check if a session has expired once a user has logged in. I have tried this every which possible way. Clearly somewhere there is a session validity check under the hood, that can be used to redirect the user back to login once their session has expired. But I cannot seem to find out what this is. When I use Auth::check() it always returns true even if the session has expired, because that does nothing more than confirm that a user is AUTHENTICATED. – Teknocat Mar 12 '20 at 03:01

2 Answers2

3

You can use the following

if(Auth::check()) { 

}
prola
  • 2,793
  • 1
  • 16
  • 15
1

Laravel will Create Session For every user no matter the User is Logged in or not. It will create Token for Specific Session. You can check if the Token Exist or not.

You can check if the token is there or not by the function Session::get('_token');.

But if you are asking if You want to check if User's Session is Expired, You can use Auth::check(). It will return if the Session is Expired or not for the specific user.

For example, If the user is Logged in, It will return True or False. But you can't check if overall Session is Expired or not Because Laravel will create and Store Session For every Single Visit No Matter if the user is Authenticated or not.

Let me know if you have any More Questions or Queries regarding Laravel Sessions. Let me know if it Helps!

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50
  • 1
    Doesn't Auth::check wether a user is logged in or not? I "just" want to put some message if the user open the page again and the session has expired – Olipol Jun 26 '17 at 07:42