Search code examples
phpmysqlhitcounter

A Proper View Counter in PHP


I have a Wordpress Plug-in with users requesting a feature that is the view counter.

I know only a few approaches on making a view counter, and the problem is that I want to optimize for performance and memory issues.

I have done a small amount of research and it seems, "mod_log_mysql", may be a great approach, but I do not have any prior knowledge on how this mod works, nor could have any ideas on how to connect it with the Wordpress Plug-in.

Or I could use a database. When the page is viewed, an update or a insert (which is said to be faster than a update) event occurs.

Thus my following option are:

  1. Update/Insert on server side when the page is called for.
  2. Research more on mod_log_mysql and find a way to connect it with the plug-in.
  3. Find a Premade view counter.

If there is a better approach, I would like to hear them in hoping this will solve my problem.


Solution

  • It really depends on what you want to achieve and how much time you want to spend on it.

    If you need more than a simple view counter per page/event, go for a premade one.

    If you need something simple, I would go for option #1.

    If you're worried about performance, use a memory table for staging the 'counts', and then have a php script move that into a regular table periodically (i.e. using a cronjob). I wouldn't expect updating a view counter in a memory table to have any significant performance impact.

    Option #2 could easily fall into premature optimization category.