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…

December 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() {
	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)) );

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

	$s = "";
	//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]) {

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


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

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

	//remove thr lock

	return $count;


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “How many users are connected?”

One thought

  1. JasonDavis says:

    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


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…

February 15, 2010

A new avatar generator for WordPress with get_avatar and Flathash

Brand new avatars for WordPress Gravatar service

April 7, 2017

3 ways to add author’s image in WordPress

Add author's image in WordPress without a plugin

April 5, 2017

Orari trenord, corri solo quando ce n’รจ bisogno

Hai presente quando corri in stazione per prendere il treno al volo e, quando arrivi, ti accorgi che il treno ha 12 minuti di ritardo?

March 30, 2017

Stop sending email to new user in WordPress admin registration

Prevent WordPress to send notification emails when you manually create new users

March 9, 2017

Add Internet Explorer class to body to detect old browser

When you have clients that still uses an old browser, you need to handle it. This javascript function detects the…

February 27, 2017