This page has been robot translated, sorry for typos if any. Original content here.

Statistics on the site. We write the visit counter script.


Surely many of you visiting various sites, pay attention to the counters that are on the sites. Statistics shows us how many people visited the resource, how many pages were shipped, how many people are on the site at the moment and so on. All this is a very useful tool, because as it allows to know the owner of the site, the relevance and rating of its resource. The more interesting and useful the resource, the more visits it has.

At the moment there are a huge number of services that offer us many tools for collecting statistics and data, as well as their analysis. All of them are very professional in carrying out their tasks, but it all started with a self-written counter that you can meet now.

If you want to understand how users are registered, I prepared a lesson in which I'll write a counter for site visits, explain how to install the counter on the site and use the data for my own purposes.


In order to write us a simple counter we will have to learn a few new functions.

    * Work with files (create / read / write);
    * Work with sessions;
    * Work with functions for creating images;
    * Connecting the counter;

We will consider three options:

    * Text counter hits;
    * Text counter of visitors;
    * Graphic counter of visitors;


Create a folder in the folder www, the folder counter. This will be the place to store the statistics and script file. Next, I will give you several options for counters.

Option 1

To use the counter, we need to create an html-page, in which we will build the script. Let it be a simple page that contains information.


<title>Моя страничка</title>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<p>Добро пожаловать</p>

Save this page in the www folder and proceed to programming the counter.


To save the received data, we need to write it somewhere, let it be a simple text file named stat.txt.

$url = "counter/stat.txt";

The variable $ url is assigned the address to the file. This is the only configuration element in which we set the required data, then the script consists of the logical else else expressions and the file handling functions

This code snippet is responsible for checking for the presence of the file, I wrote the entire course of logical actions in the comments.

if(!file_exists($url)) { //Проверка на существование файла.
//Если файла не существует,
$count = 0;
$handle = fopen($url,"a");//создаем его,
fwrite($handle,$count);// и записываем нулевое значение
fclose($handle);//Закрываем файл

Next, we describe the condition when the file is created.

else { //Если файл существует, то работаем с ним
$handle = fopen($url,"a+"); //Открываем его
$count = fread($handle,filesize($url)); //Читаем данные и присваеваем их переменной $count
fclose($handle); //Закрываем
$count++;//Добавляем одно посещение к полученному выше
$handle = fopen($url,"w"); // Открываем файл, и урезаем его до нулевой длины
fwrite($handle,$count); //Записываем переменную $count
fclose($handle); //Закрываем

In this fragment the visit counter script is described, carefully look, in the first fragment we open the file in order to open it and find out the number of visits. Next, we increase the value by 1 unit ($ count ++;) and write this value to our file.

At the end of the file, we print statistics using the echo statement

echo "количество просмотров: $count ";

We have considered the simplest type of counter, but believe me, sometimes it is more difficult and not required, for example, we need to know the number of page readings (articles, ads) inside the site. This usually indicates the most interesting material to visitors of the site. Further, in order for this counter to work, open our index.php file, and add a line to the tag