Home Links
Home Page
How to us to correct the person who has entered
Yandex - like search by the hands
Stemmer.
Creation of sites - model of qualitative imposition
Entrance parameter of function is the file from six elements
Promotion of a site in Rambler
The good design should make a profit
Validnost` HTML
Perenapravlenie mistakes in a browser - 100 % as in PHP
Terrible animal the traffic
Cajt with help HTML:: Mason
Choice of the module
Bases of
Creation of a site
Adjustment of a site
TT - the counter of the traffic
GetCurrBytes
OnOverflow
Small improvements of our counter
 

Small improvements of our counter

So, we have learned to count calls on our page. We admit{allow}, we do not want, that the file count.txt with values of the counter laid in a directory cgi-bin. Let's create on a site a separate directory, we shall name her{it}, for example, mystat where we shall copy a file count.txt. Do not forget to appoint to a file of access right 666.

Now it is necessary to specify in our script a new way to a file count.txt. Remember a line:

$namefile = "count.txt";

It would seem, unnecessary? In fact basically the name of a file could be specified and it is direct at his{its} opening. But now, when we needed to change a way to a file, it is enough to change only this line.

Let's add to the name of a file a full way to a directory with files of the server and the name of a directory in which this file at us lays. For example, if the full way looks so:

/home2/your_domen/public_html/

Let's add a name of the directory created by us and we shall receive the following:

$namefile = "/home2/your_domen/public_html/mystat/count.txt ";

We modernize. What will be, if in a browser to go on page with the counter and to press "to update"? Correctly - value of the counter will increase for unit. And each new loading of page in a browser will cause increase in value of the counter. Let's learn our counter trace calls successively from one IP-address and to not set off such calls. For this purpose we shall create a file with the name, for example, ip.txt and we shall put it{him} in the same directory mystat on the server. We shall appoint to him access rights 666. We shall store{keep} the IP-address of last visitor included by the counter in this file. Now it are necessary to add corresponding check in a script.

First of all we shall add a variable containing a way to a file:

$nameipfile = "/home2/your_domen/public_html/mystat/ip.txt ";

Now we should receive actually the IP-address of the visitor. He contains in a variable $ENV {"REMOTE_ADDR"} which value is passed a script by a browser of the visitor together with search. We shall appropriate{give} this value of a variable $ip:

$ip = $ ENV {"REMOTE_ADDR"};

Now we count from a file ip.txt value of last IP-address, we compare it{him} with received. If they coincide, then we shall not include call (having finished job of a script) if do not coincide - we shall increase value of the counter and we shall write down new value of the IP-address in a file ip.txt:



open (IPFILE, "$nameipfile");

$ipold = <IPFILE>;

close (IPFILE);

chomp ($ipold);

if ($ipold eq $ip) {

exit;

}

open (IPFILE, "$nameipfile");

print IPFILE "$ip";

close (IPFILE);


Now accuracy of indications of the counter became little bit higher. But how he will lead itself(himself) if on our page will go simultaneously two visitors? Simultaneously two processes will try to make recording for a file with indications of the counter therefore all data of a file can be lost. That it to not admit{allow}, it is necessary to forbid simultaneous access to a file of several processes. To make it it is possible with the help of function flock which allows to block a file connected to a descriptor, from access from other scripts. We shall a little change process of job with a file count.txt:



open (COUNTFILE, " + <$namefile ");

flock (COUNTFILE, 2);

$count =readline (*COUNTFILE);

chomp ($count);

$count = $ count + 1;

seek (COUNTFILE, 0,0);

truncate (COUNTFILE, 0);

print COUNTFILE "$count";

close (COUNTFILE);


Now pojasnenim our actions.

1) We open a file for reading and recordings:

open (COUNTFILE, " + <$namefile ");

2) We block a file from access from other simultaneously started processes of performance of a script

flock (COUNTFILE, 2);

3) We read - out value of the counter from a file

$count =readline (*COUNTFILE);

4) We delete a symbol of the end of a line (if he it is casually not known whence has appeared)

chomp ($count);

5) We increase value of the counter

$count = $ count + 1;

6) We move the index of a position in a file on his{its} beginning

seek (COUNTFILE, 0,0);

7) We truncate length of a file up to the current position (i.e. prior to the beginning of a file where we have moved this index in the last line)

truncate (COUNTFILE, 0);

8) We write down in a file new value of the counter

print COUNTFILE "$count";

9) We close a file, simultaneously removing with him{it} blocking

close (COUNTFILE);

So, all script will have now the following kind:



*!/usr/bin/perl

$namefile = "/home2/your_domen/public_html/mystat/count.txt ";

$nameipfile = "/home2/your_domen/public_html/mystat/ip.txt ";

$ip = $ ENV {"REMOTE_ADDR"};

open (IPFILE, "$nameipfile");

$ipold = <IPFILE>;

close (IPFILE);

chomp ($ipold);

if ($ipold eq $ip) {

exit;

}

open (IPFILE, "$nameipfile");

print IPFILE "$ip";

close (IPFILE);

open (COUNTFILE, " + <$namefile ");

flock (COUNTFILE, 2);

$count =readline (*COUNTFILE);

chomp ($count);

$count = $ count + 1;

seek (COUNTFILE, 0,0);

truncate (COUNTFILE, 0);

print COUNTFILE "$count";

close (COUNTFILE);

print " Content-Type: text/html ";

print "$count";

exit;