How to create a visitor counter in Php

Who I am
Martí Micolau
Author and references

Hold track the number of users who view our site on a daily basis is essential to understand which pages attract visitors the most. The visit report can give us valuable feedback to improve the site where possible.

Our aim will be to increment the counter not at each page load but only at each new visit by the user. If our user does not load a new page for a long time and then does it again, we can assume that he went to do something else and then returned to the site, so it makes sense to count a new visit. However, we can arbitrarily establish a reasonable time beyond which a new visit is counted, for example one hour. But we could also fix it in one day, it all depends on our needs.

How to build our little application? First of all the tools we need are a small database to keep counter values and cookie to recognize visitors and avoid counting them multiple times.

PHP hit counter

We create the MySql database (the free copy is available on the site with related documentation) by connecting to the server with the command line text client internal to MySql, with MySql Workbench or a web manager such as PhpMyAdmin.

If we use the command line text client, the SQL code to create a database "counter"Is the following:

create counter database; use counter;

Let's create a user who can safely access this database with username and password, but only from the same machine on which the database server is installed:

Grant all on contatore.*to cont@'localhost' identified by 'passcontatore';

The user is called account, with password passcontatore. If the MySql server resides on a different machine from the webserver of the site replace localhost in cont @ 'localhost' with the correct address of the server and make sure that MySql accepts connections from outside.

Once this is done we can take care of the connection to the db by creating the Php file. The name we will give is connect.php and it will have the following code:

The define command defines a constant, that is, it assigns an immutable value to a string. A constant takes up less memory and resources than a variable.

If you do not receive any errors when running this script, it means that the connection worked and we can proceed with the creation of the tables for the data. The structure to create is simple, we need a page id and a counter field. The table, therefore, will be created with an Sql code as follows:

create table conteggi (id_pagina INT NOT NULL PRIMARY KEY, visite INT NOT NULL); insert into conteggi values(0,0),(1,0),(2,0);(3,0);

To check the data entered just do:

select * from conteggi;

In the table we have entered four data, one with id_pagina = 0 which we need to count the visits to the site and three pages of which we will count the uploads. In this way, we track both visits to the site and how many times individual pages are viewed. We have only created three pages, but we can create as many as we want.

Let's move on to the actual application of the counter: the PHP code that increases the counts and shows them to us on a test page. Let's create three files called Pagina1.php, Pagina2.php, Pagina3.php with the following code for Page1.php:

We upload the files to the Web server and test them. By accessing the three pages we will see how the page counters are updated, while that of the site is updated only on the first page loaded. To see it increase, you can do a test by lowering the number of seconds in setcookie () from 3600 to 10, so after only 10 seconds of inactivity you will see the site counter refresh.

Of course there are many free tools for access analysis but building a custom counter could be useful for our needs. The tools that we can integrate into our site are available online and are:

  • Google Analytics
  • Webalizer
  • AWStats

If we do not have a MySql database, it is still possible to create a simple counter, using a text file. We create the empty file on our site contatore.txt with write permissions to anyone and, in the same directory, we create the following php file:

Let's load it in the browser and we will see the counter start from one and then update itself after an hour of inactivity.

add a comment of How to create a visitor counter in Php
Comment sent successfully! We will review it in the next few hours.