Copy link to clipboard
Copied
Please advise of any tutorial or example of a web visit counter.
I can create one that increments the count for each visit and refresh but really need something that will count unique visitors.
Maybe use a session but not sure how to do it.
Copy link to clipboard
Copied
Depending on whether this is a casual hit counter, or something more mission-critical, there's a number of ways to accomplish this.
One option is to increment the counter, then place a cookie on the user's computer. If the cookie is present, don't increment the counter again. Set the cookie to never expire, or expire after a certain window of time (after which the user would be considered "unique" again). Only drawback to this is if the user clears their cookies, then returns, the counter would consider them unique no matter what. Second, you could store each visit as a row in a database, along with the user's IP address and a date/time stamp. Your code could then query that table based on the IP address, and only increment the counter if that IP address is not present. Depending on how long you want to consider a user "unique", you could filter the rows by date. Keep in mind that this could result in a large table, depending on your traffic, so it may be best to have some sort of cut-off time after which you would delete records.
You could even combine the two methods, if you so desire.
I'm sure there are many other ways this can be accomplished, but these are two simple means to accomplish your goal.
Copy link to clipboard
Copied
Hi Oaklander;
ck's suggestions are all good, valid and will work. But need to ask, what do you consider a unique hit and what level of detail you need... cause there is a problem:
You can never 100% garantee or detect if a browser has requested your page/application before.
- users may clear caches
- users may change ip addresses
- multiple users may be behind proxies and firewalls
- same client/user may use a different browser
but mostly, a browser does not uniqely identify itself beyond vendor and version info.
So yes, ck's suggestions are great, but you need to look at what you consider a "uniqu" hit and implement your method based on that. Ultimately, you may find it best/easiest/fastest to parse the webserver log files rather than "hit counting" live [actually - google analytics does a pretty good job of that with out you writing code ]
-sean