Hit Counter?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jr
Forum Commoner
Posts: 99
Joined: Mon Mar 07, 2005 3:25 pm

Hit Counter?

Post 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)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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).
Jr
Forum Commoner
Posts: 99
Joined: Mon Mar 07, 2005 3:25 pm

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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.
Post Reply