0

I've built a simple forum which is made up of categories, topics, and posts. I would like to keep track of how many unique views each post has received. I do not suspect too much daily traffic for each post as posts are designated to specific users. If I had to estimate, I would say 500 to 1000 views (as a maximum) per day for each post; realistically only 200 - 300 views.

What I am thinking of doing:

All posts are loaded through a PHP script named topic.php. At the beginning of this script I would include some code that first checks if a COOKIE exists, and if not it would check the database for the user's IP and the post at hand. If the database found the user's IP and post-id, I would then create some COOKIE which would let the script know on the next page load no to even bother the database with the query.

Question:

  • Is storing the user's IP and post-id (perhaps along with a timestamp) the proper way to keep track of unique post views?
  • Is the method I've described above considered to be a "best practice" or even efficient by any standards?

Thanks, Evan

1 Answers1

1

I'm no expert by any means so I can't answer as to whether this is the most efficient way, but using the user's IP address may not be a great way to do it as most users will likely have a dynamic IP address, so their IP will change fairly frequently. So if you're counting on their IP being the same every time, then you won't be able to use this method.

WIll users be registering to use the forum? If so, a simple session cookie when they login should be enough for uniqueness.

Bobster4300
  • 254
  • 4
  • 15
  • Then what method should I use to store in the database to check if they've already viewed the post they are viewing? Should I just store their user-id and call it a day? –  Mar 13 '13 at 05:33
  • That's what I would typically do. Their user ID would have to be unique right so sounds like the best approach. Obviously this approach will not take unique anonymous visitors into account so it depends on how your forum is setup. If it can only be viewed by registered users then using the user ID is fine. – Bobster4300 Mar 13 '13 at 10:10