Each site will have a view of the volume. This article we will share with you a simple website traffic counter implementation, specifically as follows
First, explain the idea :
1. The user makes an access request to the server
2. The server reads the number of access files, +1, returns to the client
3. The server saves new views
4. New user access, repeat 123
Solution (main algorithm):
1. Data file: Counter.dat
2. Read the data file
Open the file;
If it does not exist, create, and take 0 as the first knowledge data;
Otherwise, read out the data;
Close the file.
3. Write the accumulated data to the file Counter.dat
accumulate data;
Open the file;
Write data;
Close the file;
4. Export the information to the Web page;
Create the Count_visitor folder.
In the Count_visitor folder, create the count_visitor.php file, and type the following code:
If necessary, you can type CSS file decorations yourself.
The variables contained in the code and their meanings
$max _len: A custom variable. The maximum number of digits of the counter;
$CounterFile: Custom variable. The file (path and file name) where the counter is stored;
$counter: Custom variable. The value of the counter;
$CF: Custom variable. Open the handle to the count file;
The functions and meanings contained in the code
File_exists ($CounterFile): Determine whether the file exists;
fopen ($CounterFile, "W"): Opens the file in write mode. (The function comes with an attribute and is created if it does not exist)
fopen ($CounterFile, ' R '): The file is opened in read-only mode.
Fgets ($CF, $max _len): reads a specified length of characters from a file
Fputs ($CF, "0"): Writes the character "0" to $CF
Fclose ($CF): Close $CF Open File
Trim (fgets ($CF, $max _len)): Removes whitespace from both sides of the removed string