Location: PHPKode > scripts > UsersOnline2 > usersonline2/UsersOnline.php
    UsersOnline ver 1.0.0
    Author: Daniel Kushner
    Email: hide@address.com
    Release: 08 Nov 2001
    Copyright 2001
    Database table scheme:
    CREATE TABLE usersonline (
        timestamp int(15) DEFAULT '0' NOT NULL,
        ip varchar(40) NOT NULL,
        file varchar(100) NOT NULL,
        INDEX (timestamp),
        INDEX ip(ip),
        INDEX file(file)

	11 March 2003 Updated by Sarah King http://www.pcpropertymanager.com
	* changed structure for speed, allowed some pages to display but not log
	* My site displays the count only on an admin page 
	* and this page flushes out the table
	* The admin is my browsers' default so I can be sure this happens frequently
	* Original Source: http://sarah.phpclasses.org/browse.html/package/382.html

class UsersOnline {

    /* public: connection parameters */
    var $host     = 'localhost';
    var $database = 'myDB';
    var $user     = 'myUserName';
    var $password = 'myPWD';
    var $page     = "";
	var $ip       = "";
    var $timeoutSeconds = 120;
    function UsersOnline($visitor=true) 
	  mysql_connect($this->host, $this->user, $this->password)
            or die('Error conecting to database');
	  //choose how you want this done depending on your version and preference
	  //$this->ip = $REMOTE_ADDR;
	  $this->ip = $_SERVER['REMOTE_ADDR'];
	  //$this->page = $PHP_SELF;
	  //$this->page = $_SERVER['PHP_SELF'];
	  $this->page = $_SERVER['REQUEST_URI'];
      if ($visitor)
    function getNumber($siteOrFile="site") 
  	  $timeout = $this->getTimeOut();
	  if ($siteOrFile == "site")
	    $sql = "SELECT DISTINCT ip FROM usersonline WHERE timestamp >= $timeout";
	    $sql = "SELECT DISTINCT ip FROM usersonline WHERE file='" . $this->page . "' and  timestamp >= $timeout";

      $result = mysql_db_query($this->database, $sql )
            or die('Error reading from database');

      return mysql_num_rows($result); 
    function printNumber($siteOrFile="site") 
	  echo $this->getNumberInfo($siteOrFile);
    function getNumberInfo($siteOrFile="site") 
	  // I use templates so I just want to get the string and pass it to
	  // my template object
	  $cnt = $this->getNumber($siteOrFile);
      if( $cnt == 1) 
         $output = "1 User online";
         $output = "$cnt Users online";
	  return $output;
    function refresh() {
        global $REMOTE_ADDR, $PHP_SELF;
		$timeout = $this->getTimeOut();
        $sql = "DELETE FROM usersonline WHERE timestamp < $timeout";
        mysql_db_query($this->database, $sql )
            or die('Error deleting from database');
	function getTimeOut()
        $currentTime = time();
        return $currentTime - $this->timeoutSeconds;	  
	function addVisitor() 
        global $REMOTE_ADDR, $PHP_SELF;
        $currentTime = time();

        $sql = "INSERT INTO usersonline VALUES ('$currentTime','" . $this->ip . "','" . $this->page . "')";
        mysql_db_query($this->database, $sql ) 
            or die('Error writing to database');                       
}//class UsersOnline

//E X A M P L E
//Remove this code and call from your proper script

$ol = new UsersOnline(false);

//get rid of the old records

//who is at my site?
//who is at this page?

$ol = new UsersOnline(true);

Return current item: UsersOnline2