PHP instance profiling: counters
Author: sucre_tiger
This counter uses text to count and does not use a database. The following functions can be implemented:
Use a text file to count multiple pages to reduce the I/O usage of the server. In the file to be recorded, you only need to add a few lines.
Code
The basic idea is as follows:
Server
Program The number of times the page is browsed from a text file. (because when all files send requests to the server, their environment variable request_uri indicates where the file comes from ......, therefore, use the environment variable request_uri of the request file to identify whether the page is being browsed .), Store the number of times and display it on the user's computer that calls this page.
First, check the data sample recorded in the data text. (red indicates the number of browsing times, and the first part is the corresponding file to be browsed)
Counter. dat/script/s2.php | 3 |/script/s1.php | 11 |/script/counter. php | 5 |/testhtml/s2.php | 7 |/testhtml/s3.php | 6 |
Now, let's go!
Counter. php
Copy code The Code is as follows: <HTML>
<Title> counter </title>
<Head>
<Body>
<? PHP
/* Define the text file for storing data */
$ Counterfile = "counter. dat \";
/* Define a tag to identify whether the data to be recorded is already in text data */
$ Sign = false;
/* Read the data into the variable $ sounterdata in array mode for backup ,*/
$ Counterdata = file ($ counterfile );
/* Use the count () function to calculate the total number of records */
/* Use the explode () function to separate $ counterdata [$ I] by symbol \ "| \" and return it to the variable $ vararray as an array */
/* The implode () function is opposite to explode (). concatenate the array $ vararray with the symbol \ "| \" and send it back to $ counterdata */
/* The environment variable $ path_info is also used
For ($ I = 0; $ I <= count ($ counterdata)-1; $ I ++)
{
$ Vararray = explode (\ "| \", $ counterdata [$ I]);
If ($ vararray [0] = $ globals [\ "request_uri \"])
{
$ Vararray [1] ++;
Print ($ vararray [1]);
$ Counterdata [$ I] = implode (\ "| \", $ vararray );
$ Sign = true;
/* Find the location of this record and use break to exit the loop */
Break;
}
}
/* Here, use the implode () function to sort the data and write it together into a text file */
/* In this way, the I/O usage of the server is reduced to the lowest point.
$ DATA = implode (\ "\", $ counterdata );
/* Open a text file and write data */
$ Fp = fopen ($ counterfile, \ "w \");
Fputs ($ FP, $ data );
/* If the data to be recorded is not in the text, Mark $ Sign = flase, add data to the text */
If (! $ Sign) {fputs ($ FP, \ "\ n \". $ globals [\ "request_uri \"]. \ "| \". \ "1 \". \ "| \");
Print (\ "1 \");
/* Close the data file */
Fclose ($ FP );
?>
</Body>
</Html>
We have already seen the working process of this program, and we all know the idea. But it is not too troublesome to write this in every file.
Don't panic! We also have powerful require () functions provided by PHP! It is much easier to use counter. php as a function. What are you waiting for? The following is the function you want:
Counter. inc Copy code The Code is as follows: <? PHP
Function counter ()
{
$ Counterfile = \ "/freespace/sure/public_html/counter. dat \";
$ Counterdata = file ($ counterfile );
$ Sign = false;
For ($ I = 0; $ I <= count ($ counterdata)-1; $ I ++)
{
$ Vararray = explode (\ "| \", $ counterdata [$ I]);
If ($ vararray [0] = $ globals [\ "request_uri \"])
{
$ Vararray [1] ++;
Print ($ vararray [1]);
$ Counterdata [$ I] = implode (\ "| \", $ vararray );
$ Sign = true; break;
}
}
$ DATA = implode (\ "\", $ counterdata );
$ Fp = fopen ($ counterfile, \ "w \");
Fputs ($ FP, $ data );
If (! $ Sign)
{
Fputs ($ FP, \ "\ n \". $ globals [\ "request_uri \"]. \ "| \". \ "1 \". \ "| \");
Print (\ "1 \");
}
Fclose ($ FP );
}
?>
The best test method is "practice". Let's look at how we can call it. Let's look at an example first:
Countertest. php
<? PHP
Require (\ "counter. inc \");
?>
<HTML>
<Head>
<Title> final version of the web page counter </title>
</Head>
<Body>
You are? Counter ();?> Reader
</Body>
</Html>
You only need to add the require () function to the file header of the HTML file to be counted, and introduce the counter () function as part of the homepage. Add where necessary <? Counter ();?> You can.
There are several points to note:
1. The file that records data must have the "write" permission. Generally, set it to "666". If the file is stored in a sub-directory, you must have the write permission for this "directory;
2. I encountered such a problem during the debugging process. INC and counter. dat is placed under a sub-directory include, and then called using the require () function under different sub-directories. The format is as follows: <? PHP
Require ("../include/counter. Inc ")
?>
However, the error "insufficient Permissions" always occurs. If you have any experts, please advise.