How to realize the traffic of php
Editor to share with you how to achieve php traffic, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Php to achieve access to the method: 1, read the data file; 2, the accumulated data into the file "counter.dat"; 3, output information to the web page.
This article operating environment: Windows7 system, PHP7.4 version, Dell G3 computer.
The realization of website visit counter by PHP
A simple website traffic counter is implemented as follows
First of all, explain the train of thought:
1. The user sends an access request to the server
two。 The server reads the access times file, + 1, and returns to the client
3. The server saves new browsing times
4. New user access, repeat 123.
Solution (main algorithm):
1. Data file: counter.dat
two。 Read the data file
Open a file
If it does not exist, create it and use 0 as the initial data
Otherwise, read the data
Close the file.
3. Write the accumulated data to the file counter.dat
Cumulative data
Open a file
Write data
Close the file
4. Output information to a web page
Create a Count_Visitor folder.
In the Count_Visitor folder, create the Count_Visitor.php file and type the following code:
Browse counter-ljccccccccccc@163.com Welcome! You are the first visitor to this site!
If necessary, you can type your own css file decoration.
Variables contained in the code and their meanings
$max_len: custom variable. The maximum number of digits of the counter
$CounterFile: custom variable. The file (path and file name) stored by the counter
$counter: custom variable. Value of counter
$cf: custom variable. Open the handle to the count file
Functions and meanings contained in the code
File_exists ($CounterFile): determines whether the file exists
Fopen ($CounterFile, "w"): opens the file as a write. (the function has its own properties, which will be created if it does not exist)
Fopen ($CounterFile,'r'): open the file read-only.
Fgets ($cf,$max_len): reads characters of a specified length from a file
Fputs ($cf, "0"): writes the character "0" to $cf
Fclose ($cf): closes files opened by $cf
Trim (fgets ($cf,$max_len)): removes spaces on both sides of a string
The above is all the content of the article "how php achieves traffic". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!