-2

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?

nick
  • 2,819
  • 5
  • 33
  • 69
  • 2
    If it was me, i would use goggle analytics, much better than any diy php hit counter. –  Mar 06 '19 at 04:00
  • I know, I would too, but the client actually wants to be able to see the hit count on the administrator panel (which is a DIY by itself lol) sooo, I'm not looking for any robust solution, just some simple stuff – nick Mar 06 '19 at 04:05
  • There's no best way especially not without any specification on the condition to be counted as 1 visit. How much traffic does the site gets matters too but yes, you need a table or at least modify your existing post table. – josephting Mar 06 '19 at 04:12
  • you can still use google analytics for that, they have an API, you can get any stats you like in any format you like to display elsewhere –  Mar 06 '19 at 04:12
  • You could write out a text file (shudder...). No, I think a quick little table would be the way to go. You could even track per page that way. – Tim Morton Mar 06 '19 at 04:14
  • Using the IP address is in the right direction. Your table column could be ip_address, page_name, today_date, visits. Check if today, IP address and page name exist. Insert only if it doesn't. This https://stackoverflow.com/questions/14104304/mysql-select-where-datetime-matches-day-and-not-necessarily-time can also help you. – hans-könig Mar 06 '19 at 04:39
  • i wouldent bother with IP, its not what you think it is. 1 IP could be thousands of people, one person could use thousands of IP's –  Mar 06 '19 at 04:54
  • It's not really useful to save IP addresses with the goal of counting unique visits. One IP have hundreds or thousands of users (like schools), and one device can get a new ip address (cellphones probably go through more than ten ip addresses a day). Much easier to just do `session_start(); $id = session_id()` and use that. – dave Mar 06 '19 at 04:56
  • @dave but I'd need a session for each post, right? – nick Mar 06 '19 at 13:15
  • you'd just need to keep track of whether that session has visited that post, so a table like `CREATE TABLE post_session_visits(post_id INT, session_id VARCHAR)` and then you can just count how many rows exist for a given post to know how many unique visitors you had for that post. – dave Mar 06 '19 at 18:16

1 Answers1

0

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.

Nico Plyley
  • 102
  • 1
  • 8