0

I have table called blog with the fields id, count, headline, text and i want to to get the n most read posts, but when a user reads an article, that is counted only once.How can this be solved using the IP address?

Sasa Jovic
  • 33
  • 1
  • 10

2 Answers2

0

You can use the superglobal variable $_SERVER, the field remote_addr contain the user IP but it isn't a secure way to count them. An other way is to count the number of user session, using the variable $_SESSION.

Antoine Pointeau
  • 399
  • 2
  • 14
0

Refer to this thread on getting IP - How to get the client IP address in PHP?

You can add another table to your DB, let's say hits with post_id (foreign key to your blog table) and ip_address as columns.

You can follow this logic afterwards.

  1. Get client IP ($ip) and current post ID ($pid)
  2. Query hits table with condition - WHERE ip_address = $ip AND post_id = $pid
  3. IF a row is returned that means article has been viewed from this IP so do nothing ELSE insert the row in table with $ip and $pid

If you need to count hits for a article, you can do a count query with post_id in where clause.

Community
  • 1
  • 1
yetanotherse
  • 500
  • 3
  • 16