I want to add a unique hit counter to my website using PHP. This counter will save visitor’s IP for each page in a database. I have a database structure like this:
Table hits with two columns:
ip
page_url
My question is: after getting the visitor’s IP in a PHP file, which is better (for performance)?
- To check if the IP address is already in the database. And when not already in the database add it
- Just add all visitors IPs (without duplicate check) and then get distinct IPs for relevant page to get the unique hit count?
If you are on MySQL you might want to abuse the combination of PRIMARY KEY and ON DUPLICATE KEY UPDATE:
Now on a page hit you do
Why this?
Really.
You may also want to record the timestamp of last access: