Counter & visitor statistics

There are lot of great website statistics systems with a lot more functions this PHP class provides, but most of them are too complex and difficult to modify. You have to use this kind of systems like provided. This script is not an alternative if you look for the same number of functions, but it’s a light weight solution to integrate into your website. Inside the latest versions (> 2.02) there is a new top list feature with the number of visits grouped by the domain name from the HTTP referrer information and a monthly detail page for visitors grouped by country and/or HTTP referrer.

PHP Script Download

Current functions

  • Unique visitor counter (time operated)
  • Registration of information about the visitor (HTTP referrer, IP address, date/time etc.)
  • Saving the visitors country code (IP address based)
  • (graphical) Visitor statistics (periods, top 10, country based…)
  • HTML table generation (statistics)
  • Block visitor access based on the a max. number of page views during a number of days


This class requires a PHP enabled Apache (virtual) host. I tested this class with PHP 4/5 and MySQL 4 and higher on a Linux web host.


  • Use both sql files to create the MySQL tables.
  • Now change the config-file “db_config.php” to fit your environment.
  • To fit the path structure check the include paths in the example / application files. I used the server variable “DOCUMENT_ROOT” as the root level.
    Above the root the is a folder named “classes”. Inside this folder I used the folder named “count_visitor_statistics” (the standard location for all files).
  • Now you can include the counter method in the required files. Tip! Use one included file for several scripts!

How to use?

It’s simple, place only a few rows of code in the top of a script and every (unique) visit is counted. Run the visitor_statistics file for all information about the counted visits.


var $referer; (string)

This variable holds the HTTP referrer information if exists. Variable is set by the class constructor

var $delay;  (default =1, integer)

The amount of time in hours before a visitor is counted again. Use a value of 24 to count unique visitor only ones day. Tip! use a 0 (zero) while testing.

var $report_by_host;  (default – true, boolean)

This new variable will show statistics for the current web host (HTTP_HOST)

Methods (Class file):


Before a new visit is registered, a check is done by this method. This function will search the database for records where the visitor with an IP address has visited the site between the current time and the number of hours defined by $delay.


Before a new visit is written to a table, the class will search for the country with this method. This query is possible with the ip2nation tables from Per Gustafson, visit his home for later updates:


After the last visit check is done (and the boolean is true) the visit will be registered with this method. The class uses some server variables inside the query.

show_all_visits(), show_visits_today(), first_last_visit($type = “last”)

These methods are used to query the database for simple visit statistics, like all visits, all visits today and the last (first visit). You can use the result of these methods on your website. Examples

echo $example->show_all_visits() // shows all visits
echo $example->show_visits_today() // shows only the visits of the day
echo $example->first_last_visit("last") // shows the last (use "first" for the first visit) visit (default = last)


This method shows the top 10 visitors grouped by a country. Together with the method res_country_top() it’s possible to create a quick view for this data.

echo $renew_password->stats_country() // shows a table with graphics, country codes and numbers


With this method is it possible to show visitor data over a period of max 12 month. The results are all visits grouped by a month.

echo $example->stats_totals() // creates a table with monthly results

stats_monthly($month, $year)

Use this method results as the daily visits. You can post the month and year to this method and you get a table with visit data.

echo $example->stats_monthly(11, 2004) // shows the visits in November 2004 in a daily format

build_rows_totals($array_labels, $array_values)

This method is used in the most reporting methods, the two parameters are used for the label (example: the day) and the value (example: visits a day). This method is only to use with other methods.

Methods (Month_detail extension)

create_date_select($element_name = “monthSelect”)

This method will create a select menu with the 12 month from the past starting with the current month. Use an if clause together with the method convert_date($req_month) to collect the month value:


month_detail_count($search_val = “nl”, $what = “country”)

Use this method for you monthly report, like:

echo $detail->month_detail_count("nl", "country"); // shows the number of visitors from The Netherlands during the selected month.
echo $detail->month_detail_count("", "referer"); // shows the number of visitors from refered by during the selected month.

Change log for Counter & visitor statistics

Version 2.06

To create statistics for several host on one web server all statistic methods are modified to query the database for the current host if the variable $report_by_host is set to true. If you use this version in an existing application you need to alter the visits table for an extra column “hostname” (check the visits.sql file). I added some style to the reporting page.

Version 2.05

There is some new function to display detailed data for month. It’s possible to show the number of visitors by single countries and by the referer information. Inside the new file “monthly_details.php” is a small extension with custom methods and also an example table for the data presentation.

Version 2.04

In this version it’s possible to show the daily average number visits inside the monthly visitors table. There also some modifications inside these methods: month_last_year() and build_rows_totals(). These methods are new: average_visits_day() and show_max_visited_day. The table names for the ip2nation table data has to be configured now inside the config.php file (constant variables). In the same time I fixed the “mini” bug about the different spelling of the table name “ip2nationCountries”.

Version 2.03

I modified the class a little because if there are visits from more then a year ago the days are counted for last 12 month too. The method stats_totals() is written to show only the date from the last year. To fix this modified the “monthly” SQL inside the method get_data_array(). The table row height at the begin of a new month for the daily statistics was very large, I fixed this inside the method stats_monthly() with the new parameter $max_height.

Version 2.02

I added a new method to obtain the topXX record count from visits with the same domain name in the referrer information. I grouped also some methods together to one method named get_data_array().

Version 2.01

I forgot to remove an old variable: $remote_adr, this var is replaced by $_SERVER[‘REMOTE_ADDR’]. The error will not occur anymore.

Version 2.00

Please read the documentation file about all new features.