I received many requests from my users asking for How to detect whether a user is online.
I found out a very simple solution using jQuery or Javascript which i've explained bellow. Please feel free to comment if you come across any issue.
The jquery part to update the user status in database
The update.php file which contains the php code to update database
I found out a very simple solution using jQuery or Javascript which i've explained bellow. Please feel free to comment if you come across any issue.

The jquery part to update the user status in database
<script>setInterval("update()", 10000); // Update every 10 seconds
function update()
{
$.post("update.php"); // Sends request to update.php
}
</script>
The update.php file which contains the php code to update database
<?php session_start();
if ($_SESSION["userid"])
{
mysql_query("UPDATE users SET lastActiveTime = NOW() WHERE userid =
$_SESSION['userid']") or die(mysql_error());
}
?>
Now lets retrieve the list of users who are "online." from database
setInterval("getList()", 10000) // Get users-online every 10 seconds
function getList()
{
$.post("getList.php", function(list)
{ $("listBox").html(list); });
}
This function requests a bit of HTML form the server every 10 seconds.The getList.php page would look like this
<?php
session_start();
if (!$_SESSION["userid"]) die; // Don't give the list to anybody not logged in
$users = mysql_query("select username,lastActiveTime from users where lastActiveTime > NOW()-60") or die(mysql_error());
$output = "<ul>";
while($row=mysql_fetch_array($query))
{
$output .= "<li>".$row["userName"]."</li>";
}
$output .= "</ul>";
print $output;
?>
That would output the list of users who are online.
Wonderful blog! Do you have any helpful hints for aspiring writers? I'm hoping to start my own site soon but I'm a little lost on everything. Would you propose starting with a free platform like Wordpress or go for a paid option? There are so many choices out there that I'm completely overwhelmed .. Any ideas? Thanks a lot!
ReplyDeleteIn my opinion wordpress is the best. I run a blog on wordpress and getting pretty good results.
DeleteSoon i'm going to publish 100+ effective ways to promote a new blog for free.
[...] 7. How to track online status of a user in my website like facebook [...]
ReplyDeletereally good post as compared to other posts thanq
ReplyDeleteam going to post in ur blog as author give me a chance my blog is http://aitamelearning.blogspot.com
Ok Vydyabhushana,
DeleteIts nice to know that you want to guest post. Send your post into our email ID 2lessons@gmail.com along with your small bio. I'll brand it and post.
hello medam
ReplyDeletewhen we put the code in the script $.post("update.php") is not work.
please give me solution my id
Pratapm8@gmail.com
Hey thanx for the code.. really simple idea..
ReplyDeleteI will modify and use it on my site..
據我們了解,這是春天,每個人都希望花更少的時間在家裡。這就是為什麼我們提供我們的援助。我們會寫文章,學期論文或文憑,你放鬆 论文代写
ReplyDeletethankyou
ReplyDelete