Web page example: you can use PHP to compile a web page counter. <P> This counter uses text to count and does not use a database. the following functions can be achieved: </P> <P> using a text file to count multiple pages reduces the I/O usage of the server to be recorded
This counter uses text to count and does not use a database. you can achieve the following functions:
Use a text file to count multiple pages to reduce the I/O usage of the server. in the file to be recorded, only a few lines of code are required.
The basic idea is as follows:
The number of times that the server program reads this page from a text file, their environment variable REQUEST_URI represents where he came from ......, therefore, the environment variable REQUEST_URI of the file is used 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. php
Counter
/* Define the text file for storing data */
$ CounterFile = 'counter. dat \';
/* Define a tag to identify whether the data to be recorded has been ** in this data */
$ Sign = False;
/* Read the data as an array into the variable $ sounterData for backup ,*/
$ CounterData = file ($ counterFile );
/* Use the count () function to calculate the total number of records */
/* Use the explode () function to exit $ counterData [$ I] with the \ '| \' 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;
/* Use break to exit the loop after finding the position of this record */
Break;
}
}
/* Here, use the implode () function to clean up the data and write it into the text file together */
/* 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 into it */
$ Fp = fopen ($ counterFile, \ 'W \');
Fputs ($ fp, $ data );
/* If the data to be recorded is not in the text and $ sign = Flase is marked, add data to the text */
If (! $ Sign) {fputs ($ fp, \ '\ n \'. $ GLOBALS [\ 'request _ URI \ ']. \ '| \'. \ '1 \'. \ '| \');
Print (\ '1 \');
/* Close the data file */
Fclose ($ fp );
?>
We have seen the working process of this program, and we all know the ideas. but it would be too troublesome to write this in every file.
Don't panic! We also have powerful require () capabilities 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
Function Counter ()