Hello. I want to make a post views counter, and then there are questions.

  1. When viewing the post, there is a request to update the "views" field. How best to identify the user who has already viewed this post.

  2. Where it is better to write the post viewed? In session or in cookie?

  • Give exact definition to viewing. If the same person looked at the article after a week, is it viewing or not? - E_p
  • Then the user needs to log in (log in) to see the articles? - E_p
  • How critical is accuracy? - E_p
  • @E_p Well, I do not even know which one is better. Just the task is to bring the most viewed posts in the category. And if the user looked through the same post a week later, then how best to proceed? View posts may even guests though users. Accuracy is not particularly required, the main thing is to get rid of the post cheat. Ie that it would be impossible to press f5 every time and the views were updated. - Alex_01
  • one
    IMHO. A list of the most widely read articles, a useless thing (it simply winds up a rating of 10 articles endlessly). It is much more useful to read in (day, week, month) or trend. But it is more difficult to implement it architecturally. - E_p

3 answers 3

You can mess around with two cookies (if there are really a lot of pages and users actually browse a lot and need tight control)

  • user_unique_id = user_unique_id уникальное_значение (token)
  • visited_post_id = id

The first cookie we generate a unique user ID (any super unique). In the visited_post_id write the unique ID of the post (or page). The user enters, js checks cookies, if there are such cookies with these keys, then send them via ajax (json) to the server and check in the mysql table if there is a user with an identifier visited_post_id in the visited table whose visited_post_id is id . That is, it turns out if you arrange the control, then save it either in the user's browser or in the database.

In fact, even if you simply store the views in cookies (in the browser), then they will fit quite a lot. If you are moving in the direction of reducing the load on the server, then it makes sense to consider the option of transferring the “scanned messages” to the client.

But if you need tight control, then a unique identifier + session + cookie + save ip in mysql. Something like this. To begin with, in any case, it will be nice to generate a unique id for the user (if there is no such one yet). Here you can see how to set cookies on jQuery.

Below is the code to create a random token for the user.

 function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } var d = new Date(); var n = d.getTime(); var token=n+getRandomInt(9,99999)+getRandomInt(1,99999); 
  • So do. The token is used in the application. - Alex_01
  1. Best to use ip address.
  2. Write better in cookies.
  • And if ip dynamic? And I wonder why it's better in the cookies? And how does it even look like? The visitor has viewed 100 posts and 100 Cook wakes him up?) - Alex_01
  • @ Alex_01 Never add extra cookies browser. The browser sends them with each request . i.e. you will be chasing the extra data by downloading each picture, css, js ... from your server! - E_p
  • @ L.Vadim Can you tell us what your statements are based on? Argue, please. And then the answer is not very useful without justification. - Vadim Ovchinnikov

You can try the eternal cookies Evercookies http://samy.pl/evercookie/ To avoid cheating, but this is certainly not the best option