SimplePageCounter

Note: The recipes here are for PmWiki versions 0.6 and 1.0 only. For PmWiki 2.0 recipes, see Cookbook.



For the equivalent recipe for PmWiki 2, see Cookbook.SimplePageCounter


Goal

Keep track of page visits.

Solution

This is a very simple page counter. For every page visited, it will increment a counter, sets a global variable $pagecount which is also available as [[$pagecount]]. The counter file (one file for each page is by default saved as $WikiDir/.$pagename.count - we have write access to $WikiDir anyway.

Attach:counter.php.txt

Discussion

As usual, add include_once("local/counter.php"); to your config.php. In your pages, you can embed it into a wikipage like

this page has been visited {$PageCount} times since dawn of mankind.

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

The counter will updated only when action=browse (or empty). So only visitors will increment the counters.
Of course there are drawbacks - you additionally clutter your $WikiDir with files. But what the heck.

If you want to get an overview about the activity on your site, run the program showcount.php - you may want to edit the paths and possibly the file matching pattern to mach what's in counter.php.

Attach:showcount.php.txt

See Also

It's used in my my homepage.

Comments & Bugs

I'd love have a counter for my Wiki :-) Currently, I get the following error message though:

Warning: fopen("wiki.d/.QmWiki/HomePage.count", "w") - Datei oder Verzeichnis nicht gefunden in /home/gerd/managementaufgaben/qualitaet/pmwiki/local/counter.php on line 19

Any ideas? --Henning

Check line 7 in the script - there you define the counter filename:

$pagecount = IncrementCounter("$WikiDir/.$pagename.count");

Looks as if you're using PATH_INFO instead of the 'traditional' question-mark way and because of that, $pagename looks a bit different. In that case you should modify the function call from

$pagecount = IncrementCounter("$WikiDir/.$pagename.count");

to

$pagecount = IncrementCounter("$WikiDir/.".str_replace("/", ".", $pagename).".count");

This will replace all slashes in $pagename with dots. Hope that helps.

--Ben

The fix worked great, thanks a lot! :-) --Henning

7 July 2004: modified counter.php with the 'Henning' fix and added clickable page links to showcount.php

Hey,

I like this counter:) But after installing it, i found out people can use reload to change the counter, which is not the way i like. So i add a cookie support. Hope u enjoy it:-) And the modified file is here

--Keith

Hi Keith,

Great idea, thanks! Is it based on the version with the 'Henning' fix? Then I'd like to install it, though I'm not sure now how it's done. Does it require the basic script to remain installed?

--Henning

Hi all,

You might want to use pphlogger for more sophisticated stats. It's main feature is that is allows to easily see which page trail a user followed. It requires php ( which shouldn't be a problem for PmWiki users ;-) and MySQL.

--Paul

Hello,

shouldn't it say include_once instead of install_once?

--Steffen

Duh! Of course! Thanks for spotting it. Corrected.

--Ben pmwiki-2.2.115 -- Last modified by {{jagtig}}

from IP: 24.149.255.225 ip should be disabled by default for security reasons