top_menu

Tuesday, 3 January 2012

How to track online status of a user in website

.

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.

track online users using jquery



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());
}

?>
This all assumes that you store your userid as a session-variable when users login to your website. Thats all about the online user status storage part.

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.

9 comments:

  1. 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!

    ReplyDelete
    Replies
    1. In my opinion wordpress is the best. I run a blog on wordpress and getting pretty good results.

      Soon i'm going to publish 100+ effective ways to promote a new blog for free.

      Delete
  2. [...] 7. How to track online status of a user in my website like facebook [...]

    ReplyDelete
  3. really good post as compared to other posts thanq
    am going to post in ur blog as author give me a chance my blog is http://aitamelearning.blogspot.com

    ReplyDelete
    Replies
    1. Ok Vydyabhushana,

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

      Delete
  4. hello medam

    when we put the code in the script $.post("update.php") is not work.
    please give me solution my id
    Pratapm8@gmail.com

    ReplyDelete
  5. Hey thanx for the code.. really simple idea..
    I will modify and use it on my site..

    ReplyDelete
  6. 據我們了解,這是春天,每個人都希望花更少的時間在家裡。這就是為什麼我們提供我們的援助。我們會寫文章,學期論文或文憑,你放鬆 论文代写

    ReplyDelete