PHPBuilder - Simple Users Online class

RSS Twitter

Simple Users Online class

by: Ilir Fekaj
January 23, 2004

Version: 1.0

Type: Class

Category: Other

License: Other

Description: This very simple class enables you to track number of visitors online in an easy and accurate manner. The class also check if user is coming behind proxy server to improve script accuracy. All this is done in real time.

Author: Ilir Fekaj
Date: December 21, 2003
Latest version & info:

This very simple class enables you to track number of visitors online in
an easy and accurate manner. It's free for all purposes, just please don't
claim you wrote it. If you have any problems, please feel free to contact me.
Also if you use it, please send me the page URL.

Example usage:

include_once ("usersOnline.class.php");
$visitors_online = new usersOnline();

if ($visitors_online->count_users() == 1) {
	echo "There is " . $visitors_online->count_users() . " visitor online";
else {
	echo "There are " . $visitors_online->count_users() . " visitors online";

Important: You need to create database connection and select database before creating object!
Table structure:
CREATE TABLE `useronline` (
  `id` int(10) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL default '',
  `timestamp` varchar(15) NOT NULL default '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id`(`id`)


class usersOnline {

	var $timeout = 600;
	var $count = 0;
	function usersOnline () {
		$this->timestamp = time();
		$this->ip = $this->ipCheck();
	function ipCheck() {
	This function checks 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 new_user() {
		$insert = mysql_query ("INSERT INTO useronline(timestamp, ip) VALUES ('$this->timestamp', '$this->ip')");
	function delete_user() {
		$delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");
	function count_users() {
		$count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));
		return $count;


Comment and Contribute

Your comment has been submitted and is pending approval.

Ilir Fekaj



(Maximum characters: 1200). You have characters left.