Show the number of viewers on a live page

Possible duplicate:
how to count site visitors using java script or php

I have a built-in stream on my website, but I want to pull out the number of live viewers on the page. Is there a way to do this using PHP / AJAX, to show the number of people browsing one of my web pages?

+3
source share
3 answers

DISCLAIMER: I did something like this a long time ago, so here is the ugly old code (that I am not going to make an effort to look better / not when I started programming, as this is just to give you an idea of ​​how this can be done, and not a false submission of any specific code).

$timeout = time() - (20);

$sessid_exist = mysql_query("SELECT sessid FROM bw_sessions WHERE sessid='" . session_id() . "'") or die (mysql_error());
$sessid_check = mysql_num_rows($sessid_exist);

if ($_SESSION['bw_username']) {
                $sql = mysql_query("UPDATE bw_sessions SET timestamp='" . time() . "', username='" . $_SESSION['bw_username'] . "' WHERE sessid='" . session_id() . "'");
} else {
  if($sessid_check > 0){
                $sql = mysql_query("UPDATE bw_sessions SET timestamp='" . time() . "' WHERE sessid='" . session_id() . "'");
              } else {
              $sql = mysql_query("INSERT INTO bw_sessions (id, username, sessid, timestamp, ip)
        VALUES(null, '', '" . session_id() . "', '" . time() . "', '" . $_SERVER['REMOTE_ADDR'] . "')") or die (mysql_error());
          }
}


$sql = mysql_query("SELECT distinct sessid FROM bw_sessions WHERE username='' AND timestamp >= '$timeout' ORDER BY timestamp DESC") or die (mysql_error());
$sql2 = mysql_query("SELECT distinct sessid,username FROM bw_sessions WHERE username!='' AND timestamp >= '$timeout' ORDER BY username DESC") or die (mysql_error());
$num_guests = mysql_num_rows($sql);
$num_reg = mysql_num_rows($sql2);
?>

<font size='1'>Currently Online: <br>
                        <?=$num_guests;?> Guests<br>
                        <?=$num_reg;?> Registered users

You just need to make a table and hold session_id .. then query this table for any "recent" action. If you want to update in real time, add the code above (changed to the table) to "online.php" and call it via jquery every x seconds or, nevertheless, you decide to do it.

+4
source

Something like Clicky should work for you.

+1
source

-, , , , -, , , .

IP- $_ SERVER ['REMOTE_ADDR'], , IP-, , . IP-, , - .

script pageload , , ajax, PHP script , , pageload, ajax , PHP.

If you still do not know how to determine how to run a PHP script in $. ajax , this is the first thing to do do, then writing a function that takes visitors into account is probably the next.

I got 244 million views in the search for such a script, both here and here .

+1
source

All Articles