Location: PHPKode > scripts > php Users Online > php-users-online/class.UsersOnline.inc
<?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)
       );

*/
    
class UsersOnline {

    /* public: connection parameters */
    var $host     = 'localhost';
    var $database = 'websapp';
    var $user     = '';
    var $password = '';
    
    var $timeoutSeconds = 120;
    var $numberOfUsers = 0;
    
    function UsersOnline() {
        $this->refresh();                                                                               
    }
    
    function getNumber() {
        return $this->numberOfUsers;
    }
    
    function printNumber() {
        if($this->numberOfUsers == 1) {
            echo "$this->numberOfUsers User online";
        } else {
            echo "$this->numberOfUsers Users online";
        }
    }
    
    function refresh() {
        global $REMOTE_ADDR, $PHP_SELF;
        
        $currentTime = time();
        $timeout = $currentTime - $this->timeoutSeconds;
        
        mysql_connect($this->host, $this->user, $this->password)
            or die('Error conecting to database');
            
        mysql_db_query($this->database, 
                       "INSERT INTO usersonline VALUES ('$currentTime','$REMOTE_ADDR','$PHP_SELF')") 
            or die('Error writing to database');                       
            
        mysql_db_query($this->database, 
                       "DELETE FROM usersonline WHERE timestamp < $timeout")
            or die('Error deleting from database');
            
        $result = mysql_db_query($this->database, 
                                 "SELECT DISTINCT ip FROM usersonline WHERE file='$PHP_SELF'")
            or die('Error reading from database');

        $this->numberOfUsers = mysql_num_rows($result); 
                                                                                     
        mysql_close();
    }
    
}

?>
Return current item: php Users Online