Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counter Problem

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
I found a nice counter script at hotscripts.com but the problem is all the counter scripts that I found are either incrementing on every reload page or locking the ip so your ip will never be counted again, never!!!

I need a counter script, since my host provides me a free counter that increments every page load! (what a host ha?)

the script that I need is to store "lets say" the last 10 ip's and then re-write on the oldest one, I get tired of looking and searching but I couldn't find any.

Can anyone modify the script that I found to store just n ips and re-write the oldest?

Code:
<?

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

// define the variables
$file = &quot;count.txt&quot;;	// text file that stores hit count
$imgExtension = &quot;gif&quot;;	// 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 = &quot;ips.txt&quot;;	// text file that stores IP addresses

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

// get the current hit count
$fp_count = fopen($file, &quot;r&quot;);
$count = fread($fp_count, filesize($file));
fclose($fp_count);

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

	// 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 . &quot;\r\n&quot;) {
			$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, &quot;a&quot;);

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

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

// if the &quot;countOnce&quot; 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, &quot;w&quot;);
	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 = &quot;0&quot; . $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(&quot;$count&quot;, $i, 1);

	// print the image for that digit
	echo &quot;<img src=images/digits/$digit.$imgExtension>&quot;;
}

?>

or any other script using MySQL to achieve my goal?
 
Modify the script to check for the presence of a cookie.

If the cookie exists, the script doesn't count the user click.

If the cookie does not exist, the script counts the user click and sets the cookie. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top