Summary: A simple page counter of visits to each page
Version: 2009-02-05
Prerequisites: pmwiki-2.2.0
Categories: Administration PHP81


Can I keep a simple page counter of visits to each page?


counter.phpΔ is a very simple page counter. For every page visited, it will increment a counter, sets a global variable $PageCount which is also available in the markup as {$PageCount}. The counter file (one file for each page is by default saved as $WorkDir/.counters/$FullName.count.


  1. Add include_once("$FarmD/cookbook/counter.php"); to your config.php. Be sure to load the counter.php script after setting your default page. --Pm July 12, 2005, at 09:33 AM
  2. Create a directory inside $WorkDir called .counters. By default $WorkDir is the same as $WikiDir (usually wiki.d), so create the directory inside wiki.d.


In your pages, you can embed it into a wikipage as follows:

This page has been visited {$PageCount} times.

This page has been visited 8185 times.

If you want to use it in a template file, simply use the $PageCount variable.

The counter will update only when action=browse. So only visitors will increment the counters.

If you prefer one Counterfile you may try the modified function (counter-dba.phpΔ) which uses a DBA-Interface. Be aware that the interfaces may vary from provider to provider. DBM usually is not supported anymore by default, but the $db_type may be db3, db4, gdbm or others (see in phpinfo and search for DBA).

  • 18-Feb-2009 DaveG: Looks like this cannot be used from with a pagelist, to refer to the number of PageCount for the current file in the pagelist. ie, something like {=$FullName$PageCount} doesn't work, nor does {=$PageCount}.
  • 18-Feb-2009 DaveG: Also seems like there would be a conflict between the variable 8185 defined by pagelist to represent the number of items processed, and the variable 8185 from this cookbook representing the number of page views.
  • HansB: Yes, it is a most unfortunate naming confusion. $PageViewCount would be more precise, and avoids confusion with pagelist's page count. Same goes for the recipe naming. I wonder if we could not just move everything to a page SimplePageViewCounter?, rename the script to pageviewcounter.php, and change the variables inside as well. Are you maintaining the script and the page, Dave? - I actually though this recipe was outdated, and surpassed by TotalCounter.
  • DaveG: Moving it (and renaming the script) to a new set of pages seems reasonable, with a redirect. I'm not maintaining it -- I implemented a bug-fix that needed doing. I'm not sure it's out-dated though. It certainly doesn't work with pagelists (which is why I stopped using it), but nor does TotalCounter. On the one hand this fills a void -- it's not as 'big' as TotalCounter; on the other hand with only 433 page views, I have to think it's not being used very much, so perhaps killing it off is the best thing :)

See Also

  • Cookbook.TotalCounter a more sophisticated statistics counter. TotalCounter currently hits a stop when the counter file grows too large, though. --Henning April 27, 2009, at 09:24 AM

Comments & Bugs

Is there a limit for the number of hits that can be counted on a single page? Once I reach 2-million-something hits, the count seems to drop back to a 1-million-something figure randomly. Or could this be the result of the file not being properly written when a server PHP process timeout occurrs? --Henning August 22, 2007, at 08:11 AM

Very late answer, but yes, the script converts counts to Integers, which maxout around 2-million.

Thanks! Maybe there is some way to extend the counting range? Probably not an issue for most pages, it's just my main portal page that is affected. --Henning February 10, 2009, at 08:26 AM ... Hm, never mind, I somehow broke my counter and have now switched over to Cookbook.TotalCounter. --Henning February 20, 2009, at 06:27 AM ... I'm back to SimpleCounter now since TotalCounter has a size limit too. --Henning April 27, 2009, at 09:24 AM

Looking at the bit count, I wonder if the 2-million-count-loss issue is really a problem of integer size - 16-bit would be 32768, and 32-bit would be about 2 billion, not 2 million ... any ideas how to fix the script to work above 2 millions? --Henning April 27, 2009, at 10:43 AM

As the count on one of my pages just dropped back to a sub-1000 number from a 10000-plus number, there might not be a fixed limit at all, but a different kind of (or additional) problem ... --Henning May 06, 2009, at 05:16 AM

The above problem is most likely a race condition updating the counter file, as no locking is done. Here's a version I did that adds file locking, and seems to work for me: (counter-paxed-20091130.phpΔ) paxed November 30, 2009, at 01:40 PM

Paxed, many thanks! Your explanation makes perfect sense to me, so I have just installed your version on my system with the full confidence that it will solve the issue once and for all! :-) --Henning December 03, 2009, at 10:56 AM

Follow-up: SimplePageCounter has in fact been running flawlessly since December 2009, registering millions of hits on tens of thousands of pages. Thanks again! --Henning August 08, 2012, at 09:10 AM

Is there a way to make a page list ordered by the number of visits? If not, does "TotalCounter" does that and are the stats transferable from one system to another? (by Larienna September 3rd, 2012)


  • Keith, original script.
  • 9-Nov-2004 Pm: Updated for PmWiki 2.
  • bhoc: Modified the counter so that it'd put the files into $WorkDir/.counters (you have to create the directory first; I didn't want to clutter the code with do-once tasks).
  • 24-Nov-2007 bhoc: Won't count non-existing files.
  • 5-Feb-2009 DaveG: Corrected code to check for existing file.

This page has been visited 8185 times.

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.