Zend - The PHP Company




Miscellaneous

Add Code


online connected checker  

Type: code fragment
Added by: fabricet
Entered: 31/07/2000
Last modified: 08/12/1999
Rating: **** (3 votes)
Views: 9455
Trick to print how many surfers are connected to a page.


<?php
/*
CREATE TABLE connected (
ip char(15) NOT NULL,
hl timestamp(14),
PRIMARY KEY (ip)
);
*/
$h="mysql.host.com";$u="user";$p="passwd";$d="database";
// disconnected timeout (seconds)
$timeout 60;
// differentiate between two surfers
$ip=getenv("REMOTE_ADDR");

mysql_connect($h,$u,$p);
// remove old connections
$q "DELETE FROM connected WHERE hl < now()";
$s mysql_db_query($d,$q);
$q "SELECT count(*) FROM connected WHERE IP='$ip'";
$s mysql_db_query($d,$q);
while ( 
$r = @mysql_fetch_array($s) ) {
$cpt $r["count(*)"];}
if (
$cpt) {
// is a reload?
$q "UPDATE connected SET hl=hl + $timeout WHERE ip='$ip'";}
else {
// welcome on board !
$q "INSERT INTO connected (ip, hl) VALUES ('$ip', now()+ $timeout)";}
$s mysql_db_query($d,$q);

// most important: connected number
$q "SELECT count(*) FROM connected";
$s mysql_db_query($d,$q);
while ( 
$r = @mysql_fetch_array($s) ) {
$cpt $r["count(*)"];}

echo 
$cpt;
?>


Usage Example


1. First create this table into your database:
CREATE TABLE connected (
ip char(15) NOT NULL,
hl timestamp(14),
PRIMARY KEY (ip)
);
2. alter the first lines of the code with your vars.
3. put the code into your page.
4. C'est fini.


Rate This Script





Search



This Category All Categories