Page 1 of 1

Current Online users

Posted: Thu Jul 05, 2007 6:15 am
by shivam0101
This is about displaying Current Online users

http://www.zeitoun.net/index.php?2007/0 ... t-with-php

Code: Select all

<?php


$query=mysql_query("SELECT * FROM current_online");
while($fetch=mysql_fetch_array($query))
{
  $username[]=fetch['username'];
}


while(1) {
  echo '<script type="text/javascript">';
 // echo 'comet.printServerTime('.time().');';
  echo 'comet.printServerTime('.time().');';
  echo '</script>';
  flush(); // used to send the echoed data to the client
  sleep(1); // a little break to unload the server CPU
}

?>
---how do i proceed next?

Posted: Thu Jul 05, 2007 7:46 am
by feyd
Proceed next?

Posted: Thu Jul 05, 2007 9:19 am
by shivam0101
what should i do to display the current user username (i.e how to pass the username)?

Posted: Thu Jul 05, 2007 10:44 am
by superdezign
First, you have to realize that '$fetch' and 'fetch' aren't the same. Do you have error_reporting turned on?

Posted: Thu Jul 05, 2007 11:22 am
by aceconcepts
To get the username why dont you put a dollar sign $ at the begining of fetch

i.e.

Code: Select all

$username[]=$fetch['username'];

Posted: Fri Jul 06, 2007 1:19 am
by shivam0101
changed the code. getting output as The server time will be shown here (this code is in index.html)

Code: Select all

$query=mysql_query("SELECT * FROM online");
while($fetch=mysql_fetch_array($query))
{
  $username[]=$fetch['username'];
}

foreach($username as $usernames)
{
  $name.=$usernames.",";   
}

//echo $name;

while(1) {
  echo '<script type="text/javascript">';
 // echo 'comet.printServerTime('.time().');';
  echo 'comet.printServerTime('.$name.');';
  echo '</script>';
  flush(); // used to send the echoed data to the client
  sleep(1); // a little break to unload the server CPU
}

?>

Posted: Sat Jul 07, 2007 2:33 am
by shivam0101
If one observes google talk, the instant a user closes his browser, Other users will see him as logged out. How this has been done?

I can understand that when a user logs out, we can enter in a flag field and check if he has logged out or not, how to do when a user closes his browser?

Posted: Sat Jul 07, 2007 7:50 am
by feyd
Google uses Ajax extensively. They likely continuously pulse check the window. Once it misses enough beats, the user is logged out.

Posted: Sat Jul 07, 2007 9:21 am
by s.dot
If they use ajax, could they not just update a script onunload() of a page?

Posted: Sat Jul 07, 2007 9:24 am
by feyd
scottayy wrote:If they use ajax, could they not just update a script onunload() of a page?
Last I checked, onunload wasn't the most reliable for making Ajax calls when the window is closing.

Posted: Sat Jul 07, 2007 10:05 am
by d3ad1ysp0rk
feyd wrote:
scottayy wrote:If they use ajax, could they not just update a script onunload() of a page?
Last I checked, onunload wasn't the most reliable for making Ajax calls when the window is closing.
+1, and in the case of a browser freeze, onunload() won't be called at all.

Posted: Wed Jul 11, 2007 7:59 am
by shivam0101
I modified the code to,

Code: Select all

<?php
...

$query_num=mysql_num_rows(mysql_query("SELECT * FROM online"));
while($query_num > 0) {

  $online='';
  $query=mysql_query("SELECT * FROM online");
  while($fetch=mysql_fetch_array($query))
  {
    //$online_arr="";
    $online[]=$fetch['userid'];
  }
  
  $online_arr=implode(',',$online);
  
 // $online_arr=time();
  
  echo '<script type="text/javascript">';
  echo "comet.printServerTime('$online_arr')";
  echo '</script>';
  flush(); // used to send the echoed data to the client
  sleep(1); // a little break to unload the server CPU

?>
It works fine. But, if i leave the window or does not do anything, it does not work. I have to refresh again to make it work. Otherwise whenever i insert or delete from database, it gets updated automatically. Can anyone tell me how to make it work?

In fact, in the orginal file also, the time runs for some 30 seconds and stops

Thanks

Posted: Thu Jul 12, 2007 2:05 am
by shivam0101
The timing problem has been solved. I had to set

Code: Select all

set_time_limit(0);
.

I need to create an infinite loop and check the condition for current online users, when i use this script, it flushes every second. Can anyone give me a better way to implement the infinite loop and check for the conditon?