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

Introduction

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 everything 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 will write a counter for site visits, explain how to install the counter on the site and use the data for my own purposes.

Details

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

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

We will consider three options:

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

Start

Create a folder in the folder www, the folder counter. This will be the location for storing the statistics and script file. Next, I will give you several variants of 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.

Index.php


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

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

Counter.php

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


&#139;?php
$url = "counter/stat.txt";

The variable $ url is assigned the address to the file. This is the only configuration element in which we specify 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 a 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 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. Usually this indicates the visitors of the site the most interesting material. Further, in order for this counter to work, open our index.php file, and add a line to the tag