what is best practise on implementing hit counters?

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
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

what is best practise on implementing hit counters?

Post by konstandinos »

hello folk

i've implemented a simple hit counter (text file with value thats incremented each time page is opened).

naturally, there are problems with this (same person navigating back and forth between site in same session increases hit counter).

technically speaking this person is still visiting the page, but it wouldnt be a fair indication of the website's popularity.

your thoughts?

i'm thinking using some kind of session within the hit counter script to track if the user has already been here in this session. and setting some kind of auto-expire date on the session (since there won't be any login/logout procedure).

i want to avoid the "track ip address within mysql" approach.

thanks.
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

I think I get what you're saying, you want a counter that only tracks unique hits and doesn't use MySQL right?

What you want to do is check if the what you're tracking is already in the file and if it isn't, then add the tracker for that person.

I do it by IP and I have both a unique hits and total hits counters, I store the contents of my hits file in a string and explode all the "\r\n", after that I strip everything except the ip from the values in the array and use the array_unique function for unique hits.

If this isn't what you're interested in, what is it that you're trying to achieve?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Save the URL that the counter is counting to a session variable like this:

Code: Select all

if(!in_array($the_URL,$_SESSION['hitcounter'])){
     $_SESSION['hitcounter'][]=$the_URL;
     // increase your count here (however you want)
}
That way the user will only be able to increase the count once per session, which seems to be your intent.

Cheers,
Kieran
konstandinos
Forum Commoner
Posts: 68
Joined: Wed Oct 04, 2006 4:20 am

Post by konstandinos »

cool thanks for the help
Post Reply