user_online.sql#
# Table structure for table 'users_online'
#CREATE TABLE `users_online` (
  `id` int(10) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL default '',
  `timestamp` varchar(15) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) TYPE=MyISAM;
user_online.php<?//
// users_online
//
// [email protected]
//
// a class that registers and counts the number of users on a website, using a timeout method/approach
///*
Copyright (C) 2002  [email protected] program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*///
// MySQL version
// for MySQL only. Replace mysql_* with the apropriate functions to access your DBMS. Other changes may be necessary.
//
class users_online { var $s_host = "localhost"; // host
var $s_username = "root"; // username
var $s_password = ""; // password
var $s_database = "office"; // database name  var $s_db_link; var $n_secs = 250;         // seconds to timeout
function users_online ( $n_ip ) { $this->n_ip = $n_ip; $this->s_db_link = mysql_connect ( $this->s_host, $this->s_username, $this->s_password );
mysql_select_db ( $this->s_database ); } //
// main
//
// calls the necessary functions in the correct order to get the desired results
// function main () {
if ( $this->is_logged ()  ) {
$this->update ();
}
else {
$this->new_user ();
} $this->clean ();
} //
// new_user
//
// inserts a user into the database
// function new_user () {
$timestamp = time ();
$sql = "insert into users_online ( id, ip, timestamp ) values ( '', '$this->n_ip', '$timestamp' )";
mysql_query ( $sql, $this->s_db_link );
} //
// is_logged
//
// checks if a user is already in the database.
// function is_logged () {
$sql = "select * from users_online where ip = '" . $this->n_ip . "'";
if ( mysql_num_rows ( mysql_query ( $sql, $this->s_db_link ) ) > 0 ) {
return 1;
}
else {
return 0;
}
} //
// clean
//
// deletes "timeouted" users from the database
// function clean () {
$n_now = time ();
$sql = "delete from users_online where timestamp < ( $n_now - $this->n_secs )";
mysql_query ( $sql );
} //
// update
//
// updates timestamp value in the database for the active user
// function update () {
$timestamp = time ();
$sql = "update users_online set timestamp = $timestamp where ip = '$this->n_ip'";
mysql_query ( $sql );
} //
// count
//
// returns the number of users in the database at runtime
// function count () {
$sql = "select * from users_online";
$count = mysql_num_rows ( mysql_query ( $sql, $this->s_db_link ) );
return $count;
}}?>count_users.php<?//
// [email protected]
//
// GPL'ed
//require_once ( "users-online.php" );//
// check if the user is behind an http proxy
//if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {
   $IP_ADDR = getenv( 'HTTP_X_FORWARDED_FOR' );
}
else if ( getenv( 'HTTP_CLIENT_IP' ) ) {
   $IP_ADDR = getenv( 'HTTP_CLIENT_IP' );
}
else {
   $IP_ADDR = getenv( 'REMOTE_ADDR' );
}$o_user = new users_online ( $IP_ADDR );  // create the object and send user's IP$o_user->main ();  // call the "main" method?><b>Number of Users:</b> <?= $o_user->count (); ?>