Check if user is logged in laravel 4

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Check if user is logged in laravel 4 on this date .

Hi I am implementing a chat/inbox feature on my website. When users are not logged in and receive a message from another user, the website will send an email to the recipient of the message. However when the user is logged in it won’t.

My questions is how do I tell whether a user is logged in or not? For example, if a user is logged in and then closes the browser my backend wouldn’t know anything.

Btw I’m building a Angularjs SPA with a restful API in Laravel 4.?

Answer

I suggest using Ajax to ping the DB when the user is logged in. Add a last_logged_in column of type Date
So when the users are logging in they update their last_logged_in field to the current date. You can do this Ajax as often as you like (very minimal performance issues). To see if a user is logged in simply check the last_logged_in date and compare it to the current date minus some time (1 minutes)..

I would not suggest using it to email users because it may result in lots of spam.
You can use the is_logged_in field in addition but don’t use it on it’s own.