Logging online status PHP / AJAX / jQuery

I am developing a networking site with users and profiles and want to be able to show when a specific user is online.

At the moment when a user logs in I log the timestamp in the users table which I do use currently through the site to determine ‘recent active’ status in a loose kind of way. I’d still want to keep that but ideally be able to replace it during a user’s session with somekind of green light online now indicator.

Trawling Google for a little while I’ve come to the conclusion that using AJAX to ping the server every 5 minutes or so during a users session, to a script that uses REPLACE INTO to insert/update records in a users_online table then on a persons file I could just call a PHP function to check whether an the user has an entry in that table.

The jQuery/AJAX I’m looking to use is;

var stillAlive = setInterval(function () {
    $.get("includes/process.php?do=online-ping&uid=<?php $_GET['uid']; ?>");
}, 50000);

Am I on the right path? Is there a better solution do you think?

I guess it depends on what else the site is doing. For example if you’re regularly delivering content to the user, and know that it is to that user, you can use whatever delivers that content to update the “last connection” date and time. But if the user is just reading a page, then some kind of timer to store it while they’re watching would seem to be a good method.

1 Like

Thanks for the help @droopsnoot - I think that is the way I am going to go.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.