...

View Full Version : Hit Counter



Blatch
09-01-2009, 10:26 AM
I can make a hit counter, but I'm just looking for tips on how I can make it so it's not abused (refresh). How can I make it so only 1 click per IP? So if you visited a url it would say "total views: 5" and if you refreshed, it would stay at 5. Only increasing to 6 from the next unique visitor.

Zangeel
09-01-2009, 10:30 AM
Blocking via IP would suck because it would only count uniques infinity. A better option is to use sessions, that way they can't refresh refresh refresh, basically when the session expires (usually when they exit a browser) then it'll count if they revisit the page.

Here's what I use:



if (!isset($_SESSION['iVisited'])) {
$_SESSION['iVisited'] = 1;
$counterfile = "counter.txt";
$fp = fopen($counterfile, "r");
$hits = fgets($fp, 10000);
fclose($fp);
$hits++;
$fp = fopen($counterfile, "w");
fputs($fp, $hits);
fclose($fp);
echo '<code>This is you\'re first visit: ' . $hits . ' hits</code>';
} else {
$counterfile = "counter.txt";
$fp = fopen($counterfile, "r");
$hits = fgets($fp, 10000);
fclose($fp);
echo '<code>You\'ve been here before: ' . $hits . ' hits</code>';
}



Then create counter.txt in the same directory, make sure it's writeable.

Blatch
09-01-2009, 06:00 PM
Thanks :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum