Home > Backend Development > PHP Tutorial > Generate visitor counter with PHP_PHP tutorial

Generate visitor counter with PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:59:07
Original
771 people have browsed it

Nowadays, more and more people are surfing the Internet, and many netizens are trying to make their own homepages, and the visitor counter is an essential part. Although many websites provide free counters, are they not made by yourself? Some friends may think it is difficult and dare not try it. In fact, with PHP as a tool, it is not difficult, and it can even be said to be very easy.

First, let me talk about the idea of ​​a visitor counter: a visitor browses this page, and the server (such as Apache) reads the number of times the page has been viewed from a document (num.txt is used as an example below), and adds 1, then save it back to num.txt, and display the number of times plus one in the browser. If another visitor browses this page, the server repeats the above process, thereby implementing a visitor counter.

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

Now we will explain the functions that the program needs to use:

1. Open file operation: int fopen(string filename, string mode);

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

String mode is the way to open the file, which must be in character form.

'r', read-only form, 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 mode, the file pointer points to the beginning of the file, the file length is truncated to 0, if the file does not exist, an attempt will be made to create the file.

'w', readable and writable, the file pointer points to the beginning of the file, and the file length is truncated to 0. If the file does not exist, an attempt will be made to create the file.

'a', append mode (write only), the file pointer points to the end of the file, if the file does not exist, an attempt will be made to create the file.

'a', readable and writable, the file pointer points to the end of the file, if the file does not exist, an attempt will be made to create the file.

2. Read file operation: string fgets(int fp, int length);

Among them, int fp is the file stream pointer to read data, and the value is returned by the fopen function.

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

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

Among them, int fp is the file stream pointer to which information is to be written, and the value is returned by the fopen function.

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

int length is the length to be written, optional. If length is not selected, the entire string will be written. Otherwise, write length characters.

4. Close file operation: int fclose(int fp);

where int fp is the file stream pointer returned by the fopen function.

Next, let’s take a look at the prototype of the counter: (assuming the num.txt file exists)


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

//Open the num.txt file in read-only mode

$num = fgets($fp,5);

//Read 4-digit number

$num ;

//Add one to the number of views

fclose($fp);

//Close file

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631956.htmlTechArticleNowadays, more and more people are surfing the Internet. Many netizens are trying to make their own homepages. Visitor counters are essential. Less part. Although many websites provide free counters, they are not...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template