This tutorial is simple, but very effective. It will count the unique hits per day and keep a permanent record. I will first explain how it works, and then I will go ahead and give you the code.
First we set up a database and put a chunk of code at the top of every page. This code will catch the users IP and write it to the database along with the date. However, when it goes to write the IP, it first searches for the IP to see if it has already visited the site, but it will only search that day. If the IP has already visited that day, it will not record it again. For example, if IP 123.456 visited on 02/04/00 it will record it, then if that IP visits again on 04/04/00 it will again record it, but if the visit twice on 02/04/00 it will not record the second time.
Let’s set up the database. Run this SQL script on whatever database you want, its not particularly large.
Now put this code at the top of every page you wish to record the hits from. This tutorial will not record hits for individual pages but will consider every page as a whole. So if they visit 2 pages that have the code in the same day it will record the IP once.
Ok, now we need to display the amount of hits. I will give you 2 options, one is displaying the hits from that day. That means it will go back to 0 everyday or you can display total unique hits recorded, not including double IP records on separate days. So in other words it will display the amount of IP’s that have viewed your site since you installed the script.
This code will display the daily hits.
This code will display the total IP’s.
If you know MySQL feel free to play around with this and display results as you wish, eg by month, year, etc.
Hope you enjoyed this tutorial, pretty simple, but effective.
