Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect online users?

Tags:

php

I'm not sure what would be the easiest way to do this. I need to be able to detect what users are online on my website. So when people are viewing a thread or something it will say next to the users name if they are ONLINE or OFFLINE. And at bottom of forum index it will say all the users who are online.

What would be the easiest approach to do something like this? I'm not sure if I will need a Javascript which would run every time a page loads or what.

Thanks :)

like image 501
Cory Avatar asked Nov 01 '10 21:11

Cory


People also ask

How can you tell if someone is offline?

We can detect if the user is online or offline by using the online property on the navigator object. Additionally, online and offline events will be triggered if the user's internet status changes.

How check user is online in PHP?

Whenever the user accesses a page, update a field in their record of the Users table last-updated-time. Then do a query for all users having a last-updated-time within the last 5 minutes. Anything more than this, and they are considered "offline."


1 Answers

  • have a MySQL database with a user table
  • have a column in that user table which saves the "last seen"-time
  • update that column on every page request
  • when checking for online or offline, check if current time minus "last seen"-time is greater than some treshold you like

Edit: You could optionally make a javascript "ping" the server (request an empty page) every two minutes or so if you want people idling with your Website open to be displayed as online, too.

like image 74
thejh Avatar answered Oct 12 '22 14:10

thejh