1

I'm using Laravel 3. How do I have a notice that can tell me how many users are currently logged in.

I wish I could explain this better but I am new to Laravel and I should upgrade to 5.1 but that is not a option at the moment.

Any help is appreciated.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
Dev1997
  • 657
  • 1
  • 5
  • 16

4 Answers4

3

Step 1: upgrade to Laravel 5.x
Step 2: add a migration to add the column active_at as a date to the users table
Step 3: add a new HTTP middleware called ActiveUser
Step 4: in the middleware handle method:

if ($user = $request->user()) {
    $user->active_at = new \DateTime();
    $user->save();
}

Step 5: feed your baby dragon
Step 6: you can now get users by activity using the active_at column

Robbo
  • 2,116
  • 1
  • 19
  • 21
  • Upgrading to Laravel 5 was the only way to really find a solution to this problem without having to use third-party tools. Thank you. – Dev1997 Apr 11 '16 at 16:19
1

I think that is not a built in feature in Laravel. You could add a column to your user table, something like a flag if the user is logged in or not.

Simply set that column to true, after authenticating of the user and to false when the user logs out again.

Where you need the number of logged in users, simply count the rows in the user table where that flag is set to true.

Reflic
  • 1,411
  • 15
  • 25
  • And what if a user doesn’t explicitly log out, i.e. they close their browser, session times out, computer loses power etc? – Martin Bean Feb 02 '16 at 13:50
  • 1
    This is a good answer, thanks. But what Martin Bean pointed out is true though. – Dev1997 Feb 02 '16 at 14:51
  • That was the reason I put that comment below. There is no way you can determine user session without a third party option ( node.js, websocket or ajax ) with pure Php or Laravel all you can get is when user started the session. if you leave browser open, or close it from the top, shut down your computer, in your system that user is still online. I would suggest looking into two solutions below. And still don't understand why it has been down voted. – serdar.sanri Apr 07 '16 at 21:06
1

As far as I know there is no easy way to achieve this. What you can do is show how many users have logged in in the last X minutes using the last logged in column in the users column. I admit, this isn't exactly what you're looking for but it might be the next best thing.

Fester
  • 863
  • 1
  • 12
  • 33
-2

There is no way to accomplish this with laravel or php without using a third party tool like html5 websockets ( ratchet, socket.io etc ) or javascript with ajax.

  • it is pretty easy and more reliable with websockets. you can create your server to handle connection/disconnection and keep record of sockets connected and emit a message to all connected sockets count of actively connected sockets. This will be real time and less transaction since websockets will only trigger when there is an activity ( someone logins/logouts ) but requires higher access level of control over server/machine in order to install node.js, socket.io and open a tcp port for communication.
  • second option is javascript with ajax. keep a column in users data table as last_online and update this data each time user browses any page in your site. Like put a code at the top/bottom of your each page and update this columns value with current time. secondly determine how much avarage time a visitor roughly spends on your each page. then create a ajax script where you show your onlines count and trigger it every x amount of time that you determined ( i.e every 30 secs ) on the other side of your code check how many people were online since your last check ( current time - x time ) for instance $query = "select count(id) total_online_last30 from users where last_check>='" . date( "Y-m-d H:i:s" , strtotime( date() . " +30 seconds") ) . "'" then print it out.
serdar.sanri
  • 2,217
  • 1
  • 17
  • 21