A simple counter class in PHP / MySQL (PIMs and visits)

The homepage counter introduced on the page can be integrated in your website by inserting only two lines of PHP code. For storing the counter state you need a MySQL database. You can optionally output the counter state on each of your pages or only on selected pages – for example in an administration section. The counter class can return the required values separately so you can insert these values at any place on your page. You can also customize the output format (output-format as HTML-formatted text) in which your counter should appear. Visits are separated by a standard timeout of 30 minutes (the visit ends after 30 minutes of idle time, in which no requests were sent from an IP-address) but you can set the visit-timeout to a value of your choice. At the next page-request from this ip-address – after the timeout – the counter counts it as a new visit.

Moreover, there is a user barrier implemented with the help of cookies. This user barrier can be activated for each user individually. With it „internal users” will not be counted as visitors and the page-impressions (page-hits) initiated by these users will be ignored by the counter too. Therewith the user can choose, if his own page-views are counted or not.

Features:

Installation and configuration

To install the counter you only have to copy the file with the counter class and, if needed, the admin-page to your webserver. You simply copy the class (file SimpleCounter.php) to a separated directory – for example /counter – and customize the inclusion of the class (require_once) with the path, where the class is stored. When creating an instance by the constructor of the class it will automatically check if the required database tables exist. If these tables don't exist they will be created automatically in the given MySQL database. The admin-page should be protected by a user authentification – the simpliest way is to use a .htaccess-file, which stores the usernames and passwords of the users who should be able to configure the counter. Therewith you can prevent unauthorized users from changing the counter-configuration.

Example

You can integrate the counter class by adding only two PHP-statements to your site. In this variant the counter will not be shown but it will count visits and page-impressions. The following example shows the integration of the php counter:

<?php
    // include the counter class
    require_once ('/counter/SimpleCounter.php');
    // create counter instance with standard session timeout
    $counter = new SimpleCounter ("localhost", "test", "test", "");
?>

In the example we assume, that the counter class ist stored in the directory /counter. The parameters of the constructor are the name of the database-server, the name of the database, the username of the database user and the password of this user. There is another optional parameter that can be used to set the visit-timeout (in seconds) to a user defined value. If you don't set this value – as in the example – with the constructor, the visit-timeout is set to the standard-value (30 minutes).

Attention:
The Counter has to be built in (with the both lines from the example above) on every single page. If you don't do this, the counter will not count all your visitors and page impressions.

If you want to show the counter-data on your page, you have to add some more php-code. The next example shows, how to print the information provided by the counter script. I think there is nothing more to say about the following code.

<?php
    // include the counter class
    require_once ('/counter/SimpleCounter.php');
    // create counter instance with standard session timeout
    $counter = new SimpleCounter ("localhost", "test", "test", "");
    // print counter state
    print "Users online: " . $counter->getUsersOnlineCount () . "<br>";
    print "Visits today: " . $counter->getTodaysVisitCount () . "<br>";
    print "Hits today: " . $counter->getTodaysHitCount () . "<br>";
    print "Counting since: " . $counter->getCounterStartDate () . "<br>";
    print "Visits total: " . $counter->getTotalVisitCount () . "<br>";
    print "Hits total: " . $counter->getTotalHitCount () . "<br>";
?>

Demonstration of the skript

At the free PHP counter script demo you can see an example of using the counter. Moreover you can configure the counter-example by an adequate administration-page.

Download

The administration page and the counter class can be found in the zip-archive. An example-page, which shows the counter-status, is in this archive too. At PHP counter class script download (4.1K) you can download the script. Please send me an e-mail if you've got any questions or suggestions.

If you like the php website counter script and you are using it on your homepage, I would be pleased if you link to my site.