How many users are connected?

This two function returns the number of users connected to your site. Since HTTP is asyncronous, the only way to…

Dicembre 3, 2009

This two function returns the number of users connected to your site.
Since HTTP is asyncronous, the only way to do this is to store the IPS and the moment when every user retrive a page.
And whenever a page is called, check if the user is already in the stored IPS, if he is already present update his timer else add him to the list.
You have to call the howManyIps() function in every page to keep the counter updated.
Watch demo, whatch source.

The first function get the IP of the connected user:

function getIP() {
	$ip="";
	if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
		else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
		else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
		else $ip = "";
	return $ip;
}

This function makes the job: store the IPS, count, store the timers.

function howManyIps() {
	$filename = "./howmanyip.log";
	$seconds = 300;
	$yourIP = getIP();

	if (file_exists($filename.".lock")) $readonly = true; else $readonly=false;

	$count = 0;
	//lock the file
	if (!$readonly) $fpLock = fopen($filename.".lock", "w");

	//read data ips
	$fp = @fopen($filename, "r");
	$arIPS=explode ("\n", @fread($fp,filesize($filename)) );
	@fclose($fp);

	//if file is locked get out
	if ($readonly) return count($arIPS);

	$s = "";
	$already=false;
	//update data and search user ip
	for ($i=0;$i<count($arIPS);$i++) {

		$arData= explode (" ", $arIPS[$i]);

		//update your user timer
		if ($yourIP==$arData[0]) {
			$already=true;
			$arData[1]=time();
		}

		// check if user is old
		if ( time()- (integer)$arData[1] < $seconds ){
			$s.=$arData[0]." ".$arData[1]."\n";
			$count++;
		}

	}

	if (!$already) {
		//your user is new, add it to the list
		$s.=$yourIP." ".time()."\n";
		$count++;
	}

	//save the list
	$fp = fopen($filename, "w");
	fwrite($fp,$s);
	fclose($fp);

	//remove thr lock
	fclose($fpLock);
	unlink($filename.".lock");

	return $count;
}

Author

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Comments on “How many users are connected?”

One thought

  1. JasonDavis ha detto:

    This is almost how I do it, the same way except I use mysql for the values instead of a file, I store the last page load time in a sessions and I check against it on every page load, if 5 minutes has past then I update the time in the online mysql table, if it is less then 5 minutes then I do nothing, so it always is up to date on whois online within a 5 minute period. I then also run a cron job to clean up the online entries for users who are gone for x amount of time and haven’t logged out

Comments are closed

Recommended

MYSQL add counter in a query

Use mysql variables to create a counter in SQL, PHP code to use an SQL counter and what does the i mean in mysqli?

Novembre 9, 2019

How to bring back tweet counters

Twitter said that deliver the tweet count for every button is too complex (costs too much) and it’s not really a correct…

Novembre 24, 2015

How many times a web link has been shared on Twitter

Twitter share button and Facebook share button are the most used buttons to share links on Internet. You can read…

Ottobre 19, 2012

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

Ottobre 8, 2012

Mixing bots to gain new services

Spiders and bots let you take services from other web sites, this could be very cool, but also this could…

Febbraio 15, 2010

Calculate dir size recursively with PHP (and count files)

This small PHP function lets you calculate the dir size entering each sub dir and making the sum of the…

Febbraio 1, 2010