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?
Asked
Active
Viewed 37 times
2 Answers
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.
- Get client IP (
$ip) and current post ID ($pid) - Query hits table with condition -
WHERE ip_address = $ip AND post_id = $pid IFa row is returned that means article has been viewed from this IP so do nothingELSEinsert the row in table with$ipand$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