2 replies [Last post]
thepineapplehead
thepineapplehead's picture
Offline
Guru
Last seen: 27 weeks 1 day ago
Joined: 2004-06-30
Posts: 9668
Points: 801

Me again. Simple php script for a graphical hit counter. Implemented at:

retro.thew00t.com/worstepisodeever/index.php

the counter script is at

retro.thew00t.com/worstepisodeever/counter/counter.php

The counter script works fine but won't display. I get this error:

Warning: fopen(count.txt): failed to open stream: No such file or directory in /home/thew00t/public_html/retro/worstepisodeever/counter/counter.php on line 39

The counter script is:

<?

////////////////////////////////////////////////////////////
//
// counter.php - a graphical counter
//
////////////////////////////////////////////////////////////
//
// This script outputs a "hit count" displayed using digit
// images and formatted according to the setting for
// "minDigits".  The hit count can be incremented on every
// page hit or only for unique IP addresses (the
// "countOnce" feature).
//
// See readme.txt for more information.
//
// Author: Jon Thomas <http://jp.thomas.name>
// Last Modified: 6/11/02
//
// You may freely use, modify, and distribute this script.
//
////////////////////////////////////////////////////////////

// define the variables
$file = "count.txt";	// text file that stores hit count
$imgExtension = "gif";	// file extension of digit images
$minDigits = 0;		// the minimum # of digits to display
			// set to 0 to display only needed digits
$countOnce = 0;		// set to 1 to count unique IPs only
			// set to 0 to count all hits
$ipFile = "ips.txt";	// text file that stores IP addresses

// DO NOT EDIT BELOW THIS POINT UNLESS YOU KNOW PHP! //

// get the current hit count

echo "<img src=images/simps/visitor.gif />";

$fp_count = fopen($file, "r");
$count = fread($fp_count, filesize($file));
fclose($fp_count);

// if the "countOnce" feature is enabled
if ($countOnce) {
	// open the IP address file
	$fp_ips = fopen($ipFile, "r");

	// compare each entry with the user's IP address
	while (!feof($fp_ips)) {
		// get an entry from the IP file
		$ip = fgets($fp_ips, 20);

		// if the user's IP matches, set the user to old
		if ($ip == $REMOTE_ADDR . "\r\n") {
			$is_old = 1;
			break;
		}

		// otherwise, set the user to new
		else {
			$is_old = 0;
		}
	}

	// close the IP address file
	fclose($fp_ips);

	// if the user is not old, add his IP to the IP file
	if (!$is_old) {
		// reopen the IP address file
		$fp_ips = fopen($ipFile, "a");

		// add the user's IP address
		fputs($fp_ips, $REMOTE_ADDR . "\r\n");

		// close the IP address file
		fclose($fp_ips);
	}
}

// if the "countOnce" feature is disabled, set the user to new
else {
	$is_old = 0;
}

// if the user is not old, increment the counter
if (!$is_old) {
	$count++;

	// save the new hit count
	$fp_count = fopen($file, "w");
	fputs($fp_count, $count);
	fclose($fp_count);
}

// count the number of digits in the hit count
$digits = strlen($count);

// if minDigits is set and the number of digits is less than minDigits, add leading zeroes
if ($minDigits && $digits < $minDigits) {
	// find the difference between minDigits and the number of digits in the count
	$diff = $minDigits - $digits;

	// add a number of leading zeroes equal to the difference
	for ($i = 0; $i < $diff; $i++) {
		$count = "0" . $count;
	}

	// set digits equal to minDigits
	$digits = $minDigits;
}

// print the appropriate image for each digit in the hit count
for ($i = 0; $i < $digits; $i++)
{
	// get a digit from the hit count
	$digit = substr("$count", $i, 1);

	// print the image for that digit
	echo "<img src=images/simps/$digit.$imgExtension />";
}

?>

Any ideas? It's been bugging me for the last hour!

Verschwindende wrote:
  • CSS doesn't make pies

Tags:
Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 2 weeks 5 hours ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

PHP Parse Errors?

Hi thepineapplehead,
Make sure the file "count.txt" exists and has the correct permissions set.

thepineapplehead
thepineapplehead's picture
Offline
Guru
Last seen: 27 weeks 1 day ago
Joined: 2004-06-30
Posts: 9668
Points: 801

PHP Parse Errors?

It definitely exists - it's in the same directory as counter.php.

Fixed it though - I just put counter.php, ips.txt and count.txt into the same dir as index.php

Cheers for your help mate!

Verschwindende wrote:
  • CSS doesn't make pies