Web Hosting Talk







View Full Version : PHP Hit Counter Question


FiftySixK
06-23-2003, 10:29 AM
I set this code up on my webpage for a half-decent hit counter.

My domain has been visited by
<?php $counter = fopen('counter.txt', 'r');
$current = fread($counter, filesize('counter.txt'));
fclose($counter);

echo $current;

$ip = getenv('REMOTE_ADDR');
$ipCheck = file('ips.txt');
if (!in_array($ip, $ipCheck)) {
$counter = fopen('counter.txt', 'w');
fwrite($counter, $current + 1);
fclose($counter);

$ipAdd = fopen('ips.txt', 'a');
fwrite($ipAdd, "\n$ip");
fclose($ipAdd);
}

if ($current > 1) {$postfix ='s';} ?>
poor soul<?php echo $postfix; ?>


It is supposed to take a single hit from every ip. I'm on dialup so to test it I disconnected and reconnected to get a new ip. The problem is it will take two hits from every ip and I'm not sure why or how. After the second hit it won't take any more from that ip, but what is allowing there to be two hits from the same ip in the first place? What do I need to do to fix this?

FiftySixK
06-23-2003, 10:33 AM
Nevermind, stupid mistake. I had my ip checking/counter incrementing and counter value reading backward.