I want to add unique hit counter to my website using PHP. So this counter will save visitor’s IP for each page in database. I have a idea about database structure like this:
1 table ( hits ) and 2 columns ( ip , page_url ). So I can get distinc IPs using simple SQL query, but i worry about performance. Is it good way to get unique hits for each page ?
In conclusin, what database structure is better for database based unique hit counter system?
EDIT :
I have 2nd question too. After getting visitor’s IP in PHP file, is it better to control if in database not this IP, then add it database or just add all visitors IPs, then get distinc IPs for relevant page for getting unique hit count ?
Thanks in advance.
This schema is sufficient (although using Redis or another memory-based data store will be much faster if you can add it to your architecture).
Use PHP’s
ip2long(before you store the IP address) andlong2ip(when you retrieve the IP address) to store IP addresses in base 32 and retrieve them as IP address strings – this allows much faster retrieval (integers are faster in query clauses, groups etc than strings).