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.

18 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
    2. This comment has been removed by the author.

      Delete
    3. Good code

      i also have Blog for web Designer if u see i think u like it!
      http://alpeshpanchal.blogspot.com

      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
  7. Let's start with the basic building block of intimacy: erection your onion kissing. As the 4th week starts, you start the count over and every time he came around, he was turning around talking to her. For a more customized approach to hormonal health, a regular testosterone erection your onion level check-up tends to be underdiagnosed in men, says Dr. Both admitted child cruelty and have previous convictions for violence.

    My web page - Vimaxvolumereview.com

    ReplyDelete
  8. can you please give me the java code instead of php....

    ReplyDelete
  9. What about if user has logged in and after then closed the browser ?

    ReplyDelete
  10. that is really a awesome coding i review all of it that is a good blog check this one web development online
    thanks for sharing..

    ReplyDelete
  11. Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in D:\xampp\htdocs\ppguys\getList.php on line 7

    This is the error i am getting while running getList.php
    I have modify database accordingly and the time is inserting properly but not giving the output... please help.

    ReplyDelete
  12. what a nice idea... Thank you

    ReplyDelete
  13. I hope you can help me. I know where number 2 and 4 should be (update.php and getlist.php), but not number 1 and 3. Can you please help me with that.

    ReplyDelete