Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the easiest way to determine if a user is online? (PHP/MYSQL)

Is there a way I can piggy back sessions to know if the user is online?

I.e: use logs on, I set a $_SESSION variable, user times out- cookie Garbage collector updates the database to update their status as offline.

EDIT: I want a solution that does not involve times or dates. I want something to ride on sessions or something similar. Guessing if someone is online is not good enough for what I need.

like image 249
MichaelICE Avatar asked Jun 27 '09 01:06

MichaelICE


People also ask

How do I know if someone 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."

How can I log a user in PHP?

php"); } // get current logged in user $logedInUsername = $_SESSION['user']; echo $logedInUsername; // check if the username is equal to admin if($logedInUsername == "admin") { echo "You are a admin!"; } else { echo "You are NOT a admin!"; } // End Require Login // ... html code below here ...


2 Answers

Don't bother with figuring out the differences between timezones. That's not necessary.

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."

If you use your server-time, via the NOW() function in MySQL, you'll side-step calculating differences between timezones.

This is the standard way of tracking how many users are presently online (Meaning, active within the last couple of minutes).

Constantly Updated

If you would like to know they are still active even when they're not jumping from page to page, include a bit of javascript to ping your server every 60 seconds or so to let you know they are still alive. It'll work the same way as my original suggestion, but it will update your records without requiring them to be frantically browsing your site at least once every five minutes.

var stillAlive = setInterval(function () {     /* XHR back to server        Example uses jQuery */     $.get("stillAlive.php"); }, 60000); 
like image 88
Sampson Avatar answered Sep 24 '22 17:09

Sampson


What you are asking for (after the clarification) is, by definition, impossible. HTTP is a connectionless protocol, so as soon as a user has hit a page and all the content comes back from the server to the user's browser, there is no connection between the two. Someone is "online" with your website for less than a second.

One thing you could do is to have JavaScript on your web page make AJAX requests back to your server on a regular basis which includes identifying information, and a different AJAX request when the user leaves the page, using window.onbeforeunload.

like image 21
dj_segfault Avatar answered Sep 22 '22 17:09

dj_segfault