Current Online users

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Current Online users

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Proceed next?
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

what should i do to display the current user username (i.e how to pass the username)?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

First, you have to realize that '$fetch' and 'fetch' aren't the same. Do you have error_reporting turned on?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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'];
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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
}

?>
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Google uses Ajax extensively. They likely continuously pulse check the window. Once it misses enough beats, the user is logged out.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

If they use ajax, could they not just update a script onunload() of a page?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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?
Post Reply