0

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.

John
  • 877
  • 5
  • 14
  • 21

2 Answers2

2

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) and long2ip (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).

Andy
  • 17,423
  • 9
  • 52
  • 69
  • thanks for your answer.(+1) 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 ? – John Jan 17 '12 at 23:17
0

I would recommend use redis database, which provides this feature out of the box and is extremely fast. Though for this you need to maintain this extra database.

Sorry, my answer is not very direct answer to your question but in case you are open to other technologies.

Puran
  • 984
  • 6
  • 15