VB icon

Counter (Advanced)

Email
Submitted on: 1/1/2015 3:53:00 AM
By: Bann (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 1628
 
     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.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Counter (Advanced)
//**************************************
Don't copy and submit as your own work please. Free for use on your site and for learning.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: 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.
// By: Bann (from psc cd)
//
// Inputs:None.
//
// Returns:Returns the number of hits to your page and a file with information on the users visiting your page.
//
// Assumes: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.
//
// Side Effects:None.
//**************************************

<?
$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
}
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.