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.
what is best practise on implementing hit counters?
Moderator: General Moderators
-
konstandinos
- Forum Commoner
- Posts: 68
- Joined: Wed Oct 04, 2006 4:20 am
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?
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?
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Save the URL that the counter is counting to a session variable like this:
That way the user will only be able to increase the count once per session, which seems to be your intent.
Cheers,
Kieran
Code: Select all
if(!in_array($the_URL,$_SESSION['hitcounter'])){
$_SESSION['hitcounter'][]=$the_URL;
// increase your count here (however you want)
}Cheers,
Kieran