Call Nucleus Development [Nd] now, 347 688 6441 or contact us


content loading...

Code Repository

The code libraries listed to the right -> are available for public use. You may copy, distribute, and eat this code as you wish. However, the license remains in effect for the life of the software.
Pretense for download and use "[Nd] Libraries":
  • You may not use this code in a commercial setting without prior consent from [Nd].
  • You may not misrepresent yourself as the author of this code.
  • You may make changes to this code, but must make the changes available under this license.
  • These libraries are governed by their respective licenses.



<?php
 
/*
 
include_once( $DIR_LIBS . 'USRONLINE.php');
$visitors_online = new USRONLINE();
 
if (count($visitors_online->error) == 0) {
 
    if ($visitors_online->countUsers() == 1) {
        echo "There is " . implode( $visitors_online->countUsers() ) . " visitor online";
    }
    else {
        echo "There are " . implode( $visitors_online->countUsers() ) . " visitors online";
    }
} else {
    echo "<b>Users online class errors:</b><br /><ul>\r\n";
    for ($i = 0; $i < count($visitors_online->error); $i ++ ) {
        echo "<li>" . $visitors_online->error[$i] . "</li>\r\n";
    }
    echo "</ul>\r\n";
}
 
 
--------------------------------------------
 
Table structure (paste this code in PHPMyAdmin or whatever program you use for db management):
sql_query("CREATE TABLE `". sql_table('usronline') ."` (
  `uid` int(10) NOT NULL auto_increment,
  `uip` varchar(15) NOT NULL default '',
  `utimestamp` varchar(15) NOT NULL default '',
  PRIMARY KEY (`uid`),
  UNIQUE KEY `id`(`uid`)
) TYPE=MyISAM COMMENT='' AUTO_INCREMENT=1 ;");
 
*/
 
 
class USRONLINE {
    
    //(objects)
    var $timeout = 600;
    var $count = 0;
    var $error;
    var $i = 0;
    var $users_online = array();
    var $match_users = null;
    
    //(constructor)
    function USRONLINE() {
        global $member;
            
        $this->timestamp = time();
        $this->ip = $this->ipCheck();
        
        $this->userId = $member->getId();
        
        if($this->userId < 1) 
            $this->userId = -1;
        
        $this->newUser();
        $this->deleteUser();
        //$this->countUsers();
            
    }
    
    
    //(public)
    function ipCheck() {
        
        /*
        This function will try to find out if user is coming behind proxy server. Why is this important?
        If you have high traffic web site, it might happen that you receive lot of traffic
        from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
        This function tryes to get real IP address.
        Note that getenv() function doesn't work when PHP is running as ISAPI module
        */
        
        if(getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        }
        elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_X_FORWARDED')) {
            $ip = getenv('HTTP_X_FORWARDED');
        }
        elseif (getenv('HTTP_FORWARDED_FOR')) {
            $ip = getenv('HTTP_FORWARDED_FOR');
        }
        elseif (getenv('HTTP_FORWARDED')) {
            $ip = getenv('HTTP_FORWARDED');
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        
        return $ip;
    }
    
    
    function matchUsers($match_users = null) {
        
        if($match_users == null)
            return false;
        
        if( !is_array( $match_users ) ) { 
            if( in_array( $match_users, $this->users_online ) )                
                return true;
            return false;        
        }        
                
        $this->match_users = $match_users;
        
        $matched = array();
        
        foreach( $this->match_users as $id )
            if( in_array( $id, $this->users_online ) )                
                $matched[] = $id;
        
        return $matched;
    }
    
    
    function countUsers() {
        
        if ( count( $this->error ) != 0 )
            return false;
        
        if( count( $this->users_online ) > 0 )
            return $this->users_online;
        
        $where = null;
        
        // if there is a provided list to match form query
            
        $ids = array();
        $res = sql_query("SELECT DISTINCT uid FROM ". sql_table('usronline') . $where, 'get all users online' );
        while ( $sr = mysql_fetch_assoc($res) ) {
            $this->users_online[] = $sr['uid'];
        }
        
        
        /*
        $ids = array();        
        while ( $sr = mysql_fetch_assoc($res) ) {
            
            if( quickQuery('SELECT COUNT(*) AS result FROM '.sql_table('member').' WHERE mnumber="'.$sr['uid'].'" AND mcanlogin="1"') )
                $ids[] = $sr['uid'];
        }    
        */
        
        //$count = mysql_num_rows($res);
        return $this->users_online;
        
    }
    
    
    //(private)
    function newUser() {
        
        if( $this->userId != -1 ) {
            
            $insert = sql_query("INSERT INTO " . sql_table('usronline') . " (utimestamp,uid,uip) VALUES ('$this->timestamp','$this->userId', '$this->ip')", 'insert user into usronline');
            
            if (!$insert) {
                $this->error[$this->i] = "Unable to record new visitor\r\n";            
                $this->i++;
            }
        }
    }
    
    
    function deleteUser() {
    
        $delete = sql_query("DELETE FROM ".sql_table('usronline')." WHERE utimestamp < ($this->timestamp - $this->timeout)", 'delete users from usronline');
        
        if (!$delete) {
            $this->error[$this->i] = "Unable to delete visitors";
            $this->i++;
        }
    }
 
}
 
?>
 

URL Buffer

The following is a change log of URL updates:

©2012. All rights reserved.