Hit Counter?
Moderator: General Moderators
Hit Counter?
Ok... I know this is probably an easy one for all you guru's out there but how would I go about making a simple hit counter? (To ++ each time a thread in a forum is viewed for example)
-
Sphen001
- Forum Contributor
- Posts: 107
- Joined: Thu Mar 10, 2005 12:24 pm
- Location: Land of the Beaver
An alternative way would be to use a database. If you have one set up, you can put the number of hits in the database and increase it by one every time the page is viewed. Both this method and the one mentioned above are about the same difficulty level. The method above would require you to look up some filesystem commands, while my method would require you to look up some db commands.
Sphen001
Sphen001
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
However, in my opinion, if you were going to keep it a basic hit counter, using a simple text file would be the best option. Using a SQL database would help, but only if you were doing advanced queries or storing other information (timestamps, IPs, etc).Sphen001 wrote:An alternative way would be to use a database. If you have one set up, you can put the number of hits in the database and increase it by one every time the page is viewed. Both this method and the one mentioned above are about the same difficulty level. The method above would require you to look up some filesystem commands, while my method would require you to look up some db commands.
Sphen001
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
i found this that i wrote a long time ago. its poorly done (i did it long ago but it still works on my website, why fix somtin that isnt broken?) so you can spruce it up a bit and make it ur own. just make a txt file named counter.txt and have the only thing in it the number 0.
just echo $output wherever you want it to show the counting.
Code: Select all
<? /*start counter*/
if (isset ($_COOKIE['countcookie'])) {
$zz = TRUE;
}else{
$zz = FALSE;
setcookie ('countcookie', $HTTP_SERVER_VARS["REMOTE_ADDR"], time()+30*60, '/', '', 0);
}
$counter = 'counter.txt';
$count = fopen($counter, 'rw');
$number = fgets($count, 999999);
fclose($count);
if ($zz == FALSE) {
$count = fopen($counter, 'w');
$number += 1;
fwrite($count, $number);
fclose($count);
}
$output = "<b><font color='#993300' size='1'>$number</b></font>";
?>I think a database is better for 2 reasons:
1) I think it takes more clock cycles to open a file, seek to the end of it, read the value, add 1 to it, write back to the file, and close the file, than it does to send an SQL update query.
2) A database can handle the page being loaded multiple times in the same instant. If the counter file is open when a second person accesses the page, the counter algorithm will find a locked counter file. A database will handle it just fine
(kind of moot points since you've already figured out how to do it but... discussion is good anyway)
1) I think it takes more clock cycles to open a file, seek to the end of it, read the value, add 1 to it, write back to the file, and close the file, than it does to send an SQL update query.
2) A database can handle the page being loaded multiple times in the same instant. If the counter file is open when a second person accesses the page, the counter algorithm will find a locked counter file. A database will handle it just fine
(kind of moot points since you've already figured out how to do it but... discussion is good anyway)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.