TotalCounter

Summary: A statistic counter - counts page views, users, languages, browsers, operating systems, referers, locations and web bots
Version: 2024-11-13
Prerequisites: PHP 8.3, PmWiki 2.3.38
Status:
Maintainer: Simon
Discussion: TotalCounter-Talk
Users: +7 -4 (View / Edit)

Question(s)

  • How can I keep track of total and single page views?
  • How can I see users activity?
  • How can I see from which country my wiki's visitors come?
  • How can I see from which sites my visitors come (who is linking my wiki)?
  • How can I see statistics on which browser/operating system my visitors use?
  • How can I see which web bots are indexing my pages?
  • My hosting service doesn't provide the logs, can I still have some site statistics?

Answer

Download totalcounter-2024-11-13.phpΔ

Caution

Requires PHP 8.3 or greater. Use previous versions for earlier versions of PHP. Some users have reported some issues. Please see the advice section below.

TotalCounter is a simplified statistics counter which counts a variety of information such as page views, users browsing, languages used, browsers used by visitors, languages, operating systems used by visitors, referers from which the visitors come from, locations from where the visitors come from, and web bots that index the pages.

It does not log single visits but just keeps the total count.

It is recommended that you backup the totalcounter.stat file before installation.


Installation

Download the script and place it in your /cookbook/ directory.

  • It is strongly recommended that you backup the totalcounter.stat file before installation.

Then add include_once('cookbook/totalcounter.php'); in your local configuration (/local/config.php).

Configuration

In config.php use the following optional configuration variables, before including the TotalCounter script (see notes below).

$TotalCounterAction can be used to change the action for the statistics page (default action is 'totalcounter').
ex.
$TotalCounterAction = 'totalcounter';
$TotalCounterAuthLevel can be used to change the authorization level necessary to see the statistics page (default authorization is 'admin').
ex.
$TotalCounterAuthLevel = 'read';
$TotalCounterMaxItems can be used to set the maximum items to be displayed under each section of the statistics page (default number of items is 30).
ex.
$TotalCounterMaxItems = 10;
$TotalCounterEnableLookup can be used to enable the DNS lookup to retrieve the user's location in case the server doesn't do it automatically (default is 0, disabled). Please note that DNS lookup may be a time consuming task.
ex.
$TotalCounterEnableLookup = 1;
$TotalCounterBlacklist is an array (of arrays) that can be used to blacklist some items from being logged (the default blacklist is empty).
The available arrays are as follows:
$TotalCounterBlacklist['Pages']
$TotalCounterBlacklist['Users']
$TotalCounterBlacklist['Browsers']
$TotalCounterBlacklist['OSes']
$TotalCounterBlacklist['Referers']
$TotalCounterBlacklist['Locations']
$TotalCounterBlacklist['Bots']
$TotalCounterBlacklist['Languages']
Regular expressions can be used for page names, users and referers, ex
$TotalCounterBlacklist['Pages'] = array('Main.HomePage','/^Site\..*/');
$TotalCounterBlacklist['Users'] = array('Admin','Paul','/^Author.*/');
$TotalCounterBlacklist['Referers'] = array('localhost');
$TotalCounterBlacklist['Locations'] = array('localhost');
$TotalCounterBarColor can be used to personalize the color of the bars in the statistics page. It is a string with the CSS syntax for color (default is '#5af').
ex.
$TotalCounterBarColor = '#000';
$TotalCounterBarColor = '#3fca3f';
$TotalCounterShowNumbers is used to show or hide the numbers next to the items in the statistics page (default is 1, enabled).

New in 1.8:

$TotalCounterEnableDownload is used to show download counts. Default is 0.
$TotalCounterDownloadManager is used to show all the download counts if you have the DownloadManager cookbook recipe installed. This contains the filename of your download manager stats file. Default is ".download.manager"
$TotalCounterGeoIP contains the filename for MaxMind's GEOIP Country ip database. This provides more accurate information on where visitors are located, and must be placed in the Wiki.d directory. Default is "GeoIP.dat".
$TotalCounterEnableGeoIP set this to 1 to enable GEOIP database lookups, and set $TotalCounterEnableLookup to 0 so that the GEOIP is used, otherwise it defaults to normal behavior (using gethostbyaddr) for trying to identify the country. Default is 0.

New in 1.9:

$TotalCounterEnableChmods (default: 1). Set this to 0 if your server doesn't allow PHP chmod() function to be run.
$TotalCounterFile sets name of data file. Defaults to "$WorkDir/totalcounter.stat"
$TotalCounterLockfile sets name of lock file. Defaults to "$WorkDir/totalcounter.lock"

New in 1.10:

$TotalCounterEnableLog (default 0). Set to 1 to enable text logging (recommended for developer use only)
$TotalCounterLogfile name and location of log file (defaults to "$WorkDir/totalcounter.log")
$TotalCounterEnableUsers (default 0). Set to 1 to enable display of users
$TotalCounterEnableCountBots (default 0), changes TotalCounter so that unless set TotalCounter only counts Bots in the Bot chart, and does not count them for OS, Location, or Referer.

Page variables

{$TotalCount}
{$PageCount} can be used in the markup to display respectively the total pages visited and the current page views ($TotalCount and $PageCount can be used in a skin .tmpl file in PmWiki versions before 2.1).
{$PageCount} is the same that is used by the SimplePageCounter, so an upgrade to TotalCounter is not possible without changing the markup (but will lose old counts). This also means that the two recipes cannot be used together (why to do it anyway?).
{$PageViews} gives the page views of the page MyPage.
Can also be used in pagelist templates.
{$TotalCounterVersion} displays the current version
{$TotalCounterMaxItems} displays the maximum count of items displayed for each chart
{$PageCountToday} displays the count of pages visited today
TotalCounter statistics

Action

The action ?action=totalcounter, added to a wiki page URL, can be used to display a statistics summary page

i18n:

The following strings are used and can be internationalized (i18n).

  • $[statistics]
  • $[Page views]
  • $[Pages]
  • $[Percent]
  • $[Count]
  • $[Users]
  • $[Browsers]
  • $[Operating systems]
  • $[Referers]
  • $[Locations]
  • $[Web bots]
  • $[today]
  • $[File Downloads]
  • $[Last Day]
  • $[Last Week]
  • $[Last Month]
  • $[Last Year]
  • $[Previous Years]

Notes:

A single text file is saved in the wiki.d directory with the name totalcounter.stat (was .total.counter in early versions) in which the data is stored. Since the directory already has write permissions, no configuration is needed.

A page view is counted only when action=browse.

When $pagename is empty ('') the recipe counts a hit to the default page Unknown, so if you want to change them in your local configuration config.php file, they must be set to the desired value before including the totalcounter.php.

For the users statistic count to work a user authentication recipe (UserAuth or AuthUser) must be installed.

For the languages statistic count to work the MultiLanguage recipe must be installed.

The totalcounter.stat file format is as follows, using the PHP function serialize:

  • the semicolon ";" separates elements.
  • the colon ":" separates values inside the same element.
  • "a" character means array, followed by the number of elements in the array and the elements enclosed in "{}" brackets.
  • "s" character means string, followed by the string length and the string enclosed in double quotes "".
  • "i" means integer, followed by a number that is the value.
Example:
a:2:{s:5:"Total";i:3406;s:5:"Pages";a:2:{s:13:"Main.HomePage";i:1412;s:9:"Main.Test";i:213;}}
Means:
array(
"Total"=>3406,
"Pages"=>array(
"Main.HomePage"=>1412,
"Main.Test"=>213
)
);

Releases

  • 2024-11-13
Fix index error, exclusively use file_get_contents, fix errors if stats are not available
  • 2024-11-10 Simon
    Refactor HTTP_USER_AGENT parsing into functions, show small percentages with 1 decimal place, add bots, OS, browser, fix logic, no longer count non-existent pages, action no longer works on non-existent pages, Auth level to view set to read
  • 2024-10-25 - Simon
    Many updates for PHP 8.3 warnings and errors, removed use of eval, more use of Lock(), more lines displayed
  • 1.12 - 2022-01-22 - Simon
    quote defined constant to remove warning, add a few more domains, bots, and Edge browser,
    use namespace, strict_types=1, use type hints, use PSFT() to replace strftime(), refactoring, fix location
  • 1.11.1 2017-10-20 Said Achmiz
    Fixed dumb bug
  • 1.11.0 2017-10-19 Said Achmiz
    Fixed $TotalCounterEnableUsers flag, it works now
    Fixed blacklist logic, it now properly blacklists things/people
  • 1.10.0 2014-01-12 totalcounter_1_10_0.phpΔ Simon (see details)
    div with class totalcounter to allow styling; friendly names for counts; don't show LastYears if zero; add logfile; log unknowns; add more robots,
    skip unknown OS if bot; skip unknown referer if bot; skip unknown location if bot; use smaller instead of small; right align percentage;
    $TotalCounterEnableGeoIp default to 0; enable https referers; use $FmtPV for page variables; Add $TotalCounterEnableUsers; add OSes; use number_format ();
    add $TotalCounterCountBots
  • 0.1 - 23.06.2005
    • First version, counts page views and total views.
  • 0.2 - 20.11.2005
    • Added action=totalcounter which displays a page with statistics summary.
  • 0.3 - 24.11.2005
    • Added logging of users, browsers, operating systems, referers and locations.
  • 0.4 - 28.11.2005
    • Optimization of the detection routines.
    • Improved detection of the user.
    • Added logging of web bots.
  • 0.5 - 02.12.2005
    • Added possibility to blacklist specific items from being logged.
    • Modified regex for better referer and location detection.
    • Added extended description of location in statistic summary.
  • 0.6 - 14.12.2005
    • Added possibility to DNS lookup the location in case the server doesn't do it automatically.
    • Added detection of location when user is sitting behind a proxy server.
    • Added possibility to blacklist with regexes for pages, users and referers.
    • Listed pages now are link to the actual page.
    • Added possibility to assign a password authorization level (edit, admin, etc).
  • 1.0 - 21.12.2005
    • Corrected a bug when the page is the default page.
    • Corrected a bug which assigned a browser when pages were crawled by a web bot.
    • Optimization of array routines.-]
    • Public release.
  • 1.1 - 03.01.2006
    • Fixed a bug when no bots are present yet.
    • Now users work with both UserAuth and AuthUser.
    • Added recognition for other popular web bots.
    • Added configuration of bars color in the statistics page.
    • Added numbers on items (configurable).
  • 1.1b - 05.01.2006
    • Fixed a bug with empty blacklist array.
    • Fixed an alignment problem in the statistics page.
    • Fixed a problem which treated Group/Page different from Group.Page.
    • Added version display in the statistics page.
  • 1.1c - 17.01.2006
    • Fixed a problem with the markup to work with 2.1.beta20.
  • 1.2 - 24.01.2006
    • Added links to profile pages for the users.
    • Reduced locking loop to 5 seconds.
  • 1.3 - 30.01.2006
    • Suppressed the modification to $pagename, now uses internal variable.
    • Fixed a bug when remote location is in upper case.
    • Changed creation of lock directory to lock file, to prevent problems with some providers.
  • 1.4 - 31.01.2006
    • Optimized the detection of the current page (using ResolvePageName()).
    • Added statistic count of languages (when used with the MultiLanguage recipe).
  • 1.4b - 20.02.2006
    • Added blacklist support for languages.
    • Some fixes about arrays.
  • 1.5 - 07.03.2006
    • Added {$PageViews} page variable.
    • Fixed a problem when ResolvePageName() function does not exist (earlier versions of PmWiki).
    • Fixed a problem with PHP version <4.3.
  • unofficial 1.6 - 27.03.2006 - totalcounter_1_6.phpΔ
    • Morison: has also added code (using PHP Sessions) to only count visitors' first visit to each page and to only count their browser, Operating System, referer and location once per session.
    • Florian Xaver:
      • Added os: "DOS"
      • Added browser: "Arachne GPL"
      • Added browser: "Blazer"
      • Changed 'palmos' to 'palm'
    • Schlaefer: a daily page counter, a short input field to set the $TotalCounterMaxItems. Changes he mades have a ## comment.
  • unofficial 1.7 - 26.07.2006 - totalcounter_1_7.phpΔ
    • Florian Xaver:
      • Fixed bug on slow servers where sometimes statistic is reseted. (when many people and/or spambots want to access this site)
      • Removed code from Morison. If you need the feature of counting once per session you have to download from link below.
  • unofficial 1.8 - 01.01.2007 - totalcounter_1_8.phpΔ
    • David Carver:
      • Added ($TotalCounterGEOIP) variable.
      • Added ($TotalCounterEnableGEOIP) - Set to 1 to use MaxMind's GEOIP Database for country identification. Make sure to turn off Lookup (set to 0).
      • Added code to get Location by looking up GEOIP from MaxMind's GEOIP Country database. You will need the PHP library to use this. Create a directory called GEOIP in the cookbook directory and place MaxMind's php files there.
      • Added code to hopefully fix resets of the file.
      • Added ignore_user_abort(true) to keep file from reseting.
      • Minor code refactoring to only open the file in write mode when action=browse
      • Added enhanced file locking mechanism to help keep counter file from reseting.
  • unofficial 1.8a - 2007-01-21 - totalcounter_1_8a.phpΔ
    • Florian Xaver (flox January 21, 2007, at 11:57 AM)
      • Improved/Fixed handling of userlanguage plug-in: (uses $userlang2 instead of $userlang)
      • Fixed handling of "File Downloads" (no "." at the filename)
  • unofficial 1.9 - 01.10.2007 - totalcounter_1_9.phpΔ
    • Mateusz Czaplinski
      • Added time statistics (last day, last month,...). Note: recognized bots are not counted (although that can be changed by commenting out line 450 (if (!isset ($tc_bot)) {) and the corresponding closing bracket on line 455)
      • Chmods can be disabled via configuration option (setting $TotalCounterEnableChmods to 0).
  • unofficial 1.9.1 - 22.01.2008 - totalcounter_1_9_1.phpΔ
    • Mateusz Czaplinski
      • A fix which tries to ensure that the site won't get locked up by TC's lockfile.
      • Added $TotalCounterFile & $TotalCounterLockfile configuration variables.
  • unofficial 1.9.2 - 08.02-2010 - totalcounter_1_9_2.phpΔ
    • Peter Bowers
      • Minor upgrade to correctly identify Google Chrome browsers
  • unofficial 1.9.2 - 10.28-2010 - totalcounter_1_9_2_php5.3.3.phpΔ
    • Nigel?
      • Minor upgrade to replace deprecated eregi() function with preg_replace() to make it php5.3.3 compliant
  • unofficial 1.9.3 - 29.10.2014 - total_counter_1.9.3.phpΔ
    • webbird
      • Replaced two occurrences of /e with Markup_e()

Version 1.10 Upgrade cautions

This update changes some defaults, please review the following:

  • ensure you backup totalcounter.stat
  • $TotalCounterEnableGeoIp now defaults to 0, if you are a GEOIP user set it to 1 in config.php
  • $TotalCounterLogfile now defaults to "$WorkDir/totalcounter.log", previously the $WorkDir was prepended after, this makes it consistent with the other file name parameters
  • Bots' OS, Referrer, and Location are no longer counted, to count bots add $TotalCounterCountBots = 1 to config.php
  • some of the recipe's page variables are created differently, check usage of them to ensure expected results

See Also

See the recipe live here) and tests here.


Contributors


Advice

TotalCounter users have had varied experience with this recipe. None-the-less improvements to this recipe by various contributors have improved its performance and reliability, making it useful for many websites.

TotalCounter may require more than the default execution time and memory limit on large and popular websites. Please configure your PHP installation accordingly.

Prior to installing TotalCounter it is advised to take a backup of the current totalcounter.stat file.

User notes +7 -4: 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.