PHPBuilder - Easy Counter

RSS Twitter
Snippets Other

Easy Counter

by: Jeff
April 21, 2003

Version: 1.0

Type: Function

Category: Other

License: GNU General Public License

Description: Simple PHP counter that requires no database, just a text file...

*								PHP - Counter (No DB)											*
*								Page: counter.php												*
*								Developer: Jeffrey M. Johns									*
*								Support:					*
*								Modified: 04/17/2003											*
This function will enable the user to put a counter on there website that
requires no database. You must pass (1) variable with this function and that
variable would be the filename of the text file to write the counter number
to. (ie. text/counter.txt) This folder and file must be set so it can be written
to. Set this folder to '666' in the CHMOD. To make the file, you can just
open notepad and save a blank .txt file to the folder you wish to house the
counter text file. Just remember to leave the text file blank to start at 0, or
you can type a number to start at that number. For instance, to start at
5011, you would type 5011 in the text file and save the file. You must also
make sure there are no extra lines (carriage returns) in the text file. To
check this just try to place you cursor under the first line with your mouse,
click under the line and if the cursor just shows up after the first line, you
are ready to go, if not, then delete the extra lines.
$path = "counter.txt";
print 'This page has been accessed ('.CallCounter($path).') times.';
Read more about PHP's reserved variables @ the following URL:
function CallCounter($path) {

	$path = trim($path);
	$no_path = '<b>CallCounter() Error:</b> You must specify a file path to enable the counter!<BR><BR>';
	$no_write = '<b>CallCounter() Error:</b> Your CHMOD access is incorrect, please set the counter text file to 666!<BR><BR>';
	$no_file = '<b>CallCounter() Error:</b> The file specified could not be opened to write the counter file to, please double check your counter file path...<BR><BR>';

	if (!$path) {											//If no variable found, write error to screen
		print "$no_path";								//Error Message
	} else {
		$counter = @file($path);						//Put contents of file into an array
		$line = @each($counter);						//Seperate each line into a division of the array					
		$counter = $line[value]+1;					//Set the counter variable
		$fp = @fopen ($path, "w");					//Open the file
			if (!$fp) {
				print"$no_file";							//Display Error Message if no file is found
				$counter = "";								//Erase Counter Number
			elseif (@fwrite ($fp, "$counter")) {		//Write the new counter value to the file
				@fclose($fp);								//Close the file
			} else {
				print "$no_write";						//Display error message if file could not be updated
				$counter = "";								//Erase Counter Number
	return $counter;

Comment and Contribute

Your comment has been submitted and is pending approval.




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