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.
Thanks a lot! This was realy helpful!
If there is only one thing i can mension it is that in the MySql dont put comma after “’00-00-0000’”.
Thans again!
You have an error in ur mysql script its:
CREATE TABLE `uniquehits` (
`ip` varchar(255) NOT NULL,
`date` date NOT NULL default ‘00-00-0000′
);
not:
CREATE TABLE `uniquehits` (
`ip` varchar(255) NOT NULL,
`date` date NOT NULL default ’00-00-0000’
);
Uh… I am not sure exactly what you are referring to as they appear the same to me. However if you are referring to the apostrophes around certain parts of the code I do apologise for that, however that is an issue associated with Wordpress stylising text and code, and it is something I do ask other people to watch out for. I will attempt to fix it.
[...] This tutorial will teach you how to create a unique hits counter, and keep the values permanently. http://designkai.com/unique-hits-counter/ [...]
RSS feed for comments on this post. TrackBack URL