简体   繁体   中英

Unique visitor hit count with PHP for multiple pages

I have a "blog" type website and I want to know how many visits each post gets (unique visitors).

I want to store this "visit" count in the "posts" table as a single integer value.

What's the best way to do this? Do I need to create another table for storing IPs and checking for duplicates or is there a simpler way to do this?

I would use Google Analytics API, but to answer the question you asked.

First create a table for "visits". You can do it in your existing table but creating a new one would be ideal. For your rows have "blogID", "IP", "hits". You can name your rows accordingly.

I would create a function in PHP for trackViews() and pass the IP address and the current blog post identifier.

Example:

function trackViews($IPAddr, $id) {...}

Inside that function first, check to see if the IP has already been stored under that blog post

SELECT * FROM `visits` WHERE `IP` = $IPAddr & `blodgID` = $id

If the database returns no results, then add the IP to the database which will track unique views.

Now if it returns data, update the database hits row to increment +1 to track hits from the same IP address. The reason is not only to track all views but public wifi may also get multiple hits which would all be from the same IP address.

You can still display data easily either in a cross join or just two separate SQL queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM