Generate a visitor counter using PHP

Source: Internet
Author: User

Nowadays, there are more and more people accessing the internet. Many netizens try to make their own home pages. Visitor counters are an essential part. Although many websites provide free counters, are they not produced by yourself? Some friends may think it is difficult and dare not try it. In fact, with the PHP tool, it is not difficult, or even easy to say.

First, let's talk about the concept of visitor counters: refer, and display the number of times after adding one in the browser. If another visitor browses this page, the server repeats the above process to implement the visitor counter.

PHP does not have direct counter functions, but with its powerful functions, we can easily write a counter by ourselves.

The functions required by the program are described as follows:

1. open the file: int fopen (string filename, string mode );

Here, string filename is the name of the file to be opened, which must be in the string format. For example, "num.txt ".

String mode is used to open a file. It must be a character.

'R' in read-only mode. The file Pointer Points to the beginning of the file.

'R', readable and writable. The file Pointer Points to the beginning of the file.

'W': Write-only. The file Pointer Points to the beginning of the file, and the file length is cut to 0. If the file does not exist, the file will be created.

'W': readable and writable. The file Pointer Points to the beginning of the file and cut the file length to 0. If the file does not exist, the file will be created.

'A', In the append format (only writable). The file Pointer Points to the end of the file. If the file does not exist, the file will be created.

'A', readable and writable. The file Pointer Points to the end of the file. If the file does not exist, the system will try to create a file.

2. File Read operations: string fgets (int fp, int length );

Here, int fp is the file stream pointer to read data, which is returned by the fopen function.

Int length is the number of characters to be read, and the actual number of characters to be read is length-1.

3. file write operations: int fputs (int fp, string str, int [length]);

The int fp is the file stream pointer to write information, which is returned by the fopen function.

String str is the string of the file to be written.

Int length is the write length. Optional. If length is not selected, the entire string is written. Otherwise, write length characters.

4. Disable the file operation: int fclose (int fp );

Int fp is the file stream pointer returned by the fopen function.

Next, let's take a look at the counter prototype: (the hypothetical num.txt file exists)

<? Php

$ Fp = fopen ("num.txt", "r ");

// Open the num.txt file only.

$ Num = fgets ($ fp, 5 );

// Read four digits

$ Num;

// Browsing times plus one

Fclose ($ fp );

// Close the file

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.