Login   Register  
PHP Classes
elePHPant
Icontem

File: class.UsersOnline.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Daniel Kushner  >  Users Online  >  class.UsersOnline.inc  >  Download  
File: class.UsersOnline.inc
Role: ???
Content type: text/plain
Description: Reports the number of users that are currently online.
Class: Users Online
Author: By
Last change:
Date: 2002-02-03 18:51
Size: 2,260 bytes
 

Contents

Class file image Download
<?php
 
/*
    UsersOnline ver 1.0.0
    Author: Daniel Kushner
    Email: daniel@websapp.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();
    }
    
}

?>