README.md

December 5, 2022 ยท View on GitHub

Counter (Advanced)

Description

Code for your very own advanced hit counter. Not only does it count hits, it also gets stat information on the user such as Browser and OS. Great for beginners learning basic PHP functions and PHP Environment variables. Commented well. Spaced for easier reading. Compatible with PHP 3.x and 4.x and works on all OS's.

More Info

Copy and paste the code below into a file named count.php. The dir your counter file (NOT count.php, the file that will save the count) is in needs to be chmoded 777. Just use the include command (include("count.php");) and then call it (echo Count();). To see the userstats, goto the dir with the counter file (NOT count.php) and look at stats.txt with your favorite text editor.

Returns the number of hits to your page and a file with information on the users visiting your page.

Submitted On
ByBann
LevelBeginner
User Rating4.7 (14 globes from 3 users)
CompatibilityPHP 3.0, PHP 4.0
CategoryServer Side
WorldPHP
Archive File

API Declarations

Don't copy and submit as your own work please. Free for use on your site and for learning.

Source Code

<?
$countfile = "counter.txt";	// The file where the count is stored. Doesn't have to be created!
$statsfile = "stats.txt";	// The file where the user stats are stored. Doesn't have to be created!
/****No need to change anything below here****/
if (file_exists($countfile)) { 			// Makes sure the file above exists, if it doesn't, goto else
	$fp = fopen($countfile, "r"); 			// Open the file above ($countfile)
	 $output = fread($fp, filesize($countfile)); 	// Read the file and get the count
	fclose($fp);					// Close the file above
	$count = intval($output);			// Make sure the value read from the file ($output) is a number
}
else { 						// No file was found! These instructions are executed instead
	$count = 0; 					// Set the count to 0 since there was no file
}
// This is the function used to add then display the count on your page and to get the stats on the user
function ShowCount() {
	global $ShowCount, $countfile, $statsfile, $count; // Makes sure the count is displayed on all systems
	$month 	= date(m);					// This gets the current month
	$day 	= date(d);					// This gets the current day
	$year 	= date(Y);					// This gets the current year
	$hour 	= date(G);					// This gets the current hour
	$minute	= date(i);					// This gets the current minute
	$second	= date(s);					// This gets the current second
	$date = "$month/$day/$year $hour:$minute:$second\n";	// This is the date used in the stats file
	$ipaddress = getenv("REMOTE_ADDR");	// This is the user's IP. getenv() is used to get an environment variable
	$otherinfo = getenv("HTTP_USER_AGENT");	// This is other info on the user like their browser and OS platform.
	$fp = fopen($statsfile, "a");						// This opens the file as "a", which means it adds to the end
	 fwrite($fp, "IP: $ipaddress | Info: $otherinfo | Date: $date");	// This is the user info being saved.
	fclose($fp);								// Close the file
	$count++; 				// Add 1 count
	$fp = fopen($countfile, "w"); 		// Open the file above ($countfile)
	 fwrite($fp, $count); 			// Save the new count to the file
	fclose($fp); 				// Close the file above
	$ShowCount = $count; 			// Save the value loaded ($count) as $ShowCount so it can be returned correctly
	return $ShowCount; 			// Tells PHP to return that value
}
?>