PHP implements Web site traffic counter

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.