Count message views

I am working on a project where only the message heading is displayed on the main page and when you click the heading, the full post is uploaded to another posts.php page for this code:

 <a href="posts.php?postId=<?php echo $row['posts_id'] ?>"><?php echo $row['title']; ?></a> 

Now, to count post messages, I have a hits column in my posts table, initially the hits value is set to 0 , and whenever the message opens, the hits value is increased by 1, for this my code is in posts.php

 $id = $_GET['postId']; $sql = "UPDATE posts SET hits = hits + 1 WHERE post_id = $id"; 

But this is not a good practice for tracking message views, since views are updated when the page is refreshed. I want a clean system to track message views when it increases by one for each individual user or viewer, regardless of how many times the same user / visitor views the same message (as in stackoverflow). Like tracking them by their IP address or something else, just an idea (how these guys do it) or how the stuff works will be enough for me to get started.

+5
source share
3 answers

You cannot solve your problem so simply. Your problem is counting the unique users who are viewing the page (with perhaps a time component). You have a few problems as described in the comments. The first is what you think. For the prototype, the IP address is good, like everything else to get started. However, it has many short circuits, and you will need to think about identifying the “visitor”.

There is a way to solve your problem in SQL, something kind of efficient. But this requires an additional table at the post / visitor level. It will have one row per combination, and then you will need to count from there. To quickly get a unique score, you will need the insert trigger in this table.

Here is a sketch of the code:

 create unique index unq_postvisitors_post_visitor on postvisitors(postid, visitorid); insert into PostVisitors (postid, visitorid) select $postid, $visitorid on duplicate key update set counter = counter + 1; delimiter $$ create trigger trig_PostVisitors after insert on PostVisitors begin update posts set numvisitors = numvisitors + 1 where posts.post_id = new.post_id; end;$$ delimiter ; 
+2
source

The easiest way that I use to solve this problem is with cookies.

Whenever your page is open, you check to see if cookie_name cookie is set via isset($_COOKIE[$cookie_name]) .

If isset returns false, you set a cookie through setcookie(cookie_name, value, expire); maybe set the expiration time to 24 hours (you should set it in seconds, so 24 hours is 84600). In addition, you run your counting system from +1 to the visitor counter.

If isset returns true, do nothing.

PHP Cookies Refs

+1
source

Try it, it will work

 $refreshed = $_SERVER['HTTP_CACHE_CONTROL']; if ($refreshed == 'max-age=0'){ $sql = "UPDATE posts SET hits = hits + 1 WHERE post_id = $id"; } 

Try this script on the page $_SERVER['HTTP_CACHE_CONTROL'] if the page is refreshed

0
source

Source: https://habr.com/ru/post/1235946/


All Articles