Page 1 of 1
Hit Counter?
Posted: Thu Apr 14, 2005 1:46 pm
by Jr
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)
Posted: Thu Apr 14, 2005 1:53 pm
by shiznatix
easiest is use a text document, then when the page is opened it checks the number in the text document, adds 1 to it, then rewrites the number in the document. simple and easy.
Posted: Thu Apr 14, 2005 2:02 pm
by Sphen001
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
Posted: Thu Apr 14, 2005 3:02 pm
by d3ad1ysp0rk
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
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).
Posted: Thu Apr 14, 2005 3:21 pm
by Jr
yeah, I figured it out right after I posted (duhh!) I am using a DB so I just added a hit_count field and incremented it every time thread was viewed.
Posted: Thu Apr 14, 2005 3:26 pm
by shiznatix
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.
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>";
?>
just echo $output wherever you want it to show the counting.
Posted: Thu Apr 14, 2005 5:00 pm
by pickle
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)
Posted: Fri Apr 15, 2005 7:43 am
by Sphen001
Both options are good. IMO it's not worthwile to create a DB for just one hit counter, but if you've got one set up already, it may be easier than a file. It all depends on your individual circumstances.