Location: PHPKode > scripts > Users Online by IP > users-online-by-ip/usersOnline.php
<?
	class usersOnline{
		var $path = "/full/path/to/this/class/"; //full path to this class including trailing slashes
		var $minutes = 5; // time in minutes for a user to be deemed on the website
		var $historyFile = 'history.txt';
		var $historyArray = array();
		var $onlineCount = 0;	
		
		function getUsers($ip){
			$handle = fopen($this->path . $this->historyFile, 'r');		
				
			$history = '';			
			while (!feof($handle)) {
				$history .= fread($handle, 8192);
			}			
			
			fclose($handle);
			
			$this->historyArray = explode('|', $history);
			
			$pos = strpos($history, $ip);
			
			if ($pos === false){
				$this->historyArray[sizeof($this->historyArray)] = $ip.','.time();
			}
		}
		
		function updateHistory(){
			if ($handle = fopen($this->path . $this->historyFile, 'w+')){
				$historyText = '';
				
				foreach($this->historyArray as $i => $v){
					if (strlen(trim($v)) > 0){
						$user = split(',', $v);
						$historyText .= $user[0].','.$user[1].'|';
					}
				}
				
				$historyText = substr($historyText, 0, strlen($historyText) - 1);
				
				if (fwrite($handle, $historyText)){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		
		function howMany($ip){
			$this->getUsers($ip);
			$this->onlineCount = 0;
			
			foreach($this->historyArray as $i => $v){
				$user = split(',', $v);
				
				if ($user[1] > (time() - $this->minutes * 60) || $user[0] == $ip){
					$this->onlineCount++;
				}else{
					$historyArray[$i] = '';
				}
			}
			
			$this->updateHistory();
			return $this->onlineCount;
		}
	}
?>
Return current item: Users Online by IP