Generate Guest counters with PHP

Source: Internet
Author: User
Tags numeric numeric value readable truncated

Now, more and more people surf the internet, many netizens try to make their own homepage, the visitor counter is an essential part. Although many websites offer free counters, can they not be made by themselves? Some friends may think it is difficult, do not dare to try, in fact, with the tool of PHP, it is not difficult, even can say it very easy.

First of all, let me talk about the visitor counter's thinking: A visitor browses this page, and the server (such as Apache) reads the number of times the page has been browsed from a document (Num.txt, for example), adds one, and then saves it back to Num.txt and displays the number of times it was added in the browser. If another visitor browses this page, the server repeats the above process and realizes the visitor counter.

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

The function to be used by the program is described:

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

where string filename is the name of the file to open and must be a string form. For example, "Num.txt".

String mode is the way the file is opened and must be a character form.

' R ', read-only form, and the file pointer points to the beginning of the file.

' R ', readable and writable, and the file pointer points to the beginning of the file.

' W ', write-only form, the file pointer points to the beginning of the file, the length of the file is truncated to 0, if the file does not exist, will attempt to establish the file.

' W ', readable to write, file pointer to the beginning of the file, the length of the file truncated to 0, if the file does not exist, will attempt to establish the file.

' A ', the Append form (writable only), the file pointer points to the end of the file, if the file does not exist, will attempt to establish the file.

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

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

where the int FP is the file stream pointer to be read into the data, the Fopen function returns the numeric value.

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

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

where the int FP is the file stream pointer to write information, the fopen function returns a numeric value.

String STR is the strings to be written to the file.

The length of int is written, optionally, if length is not selected, the entire string is written. Otherwise, the length character is written.

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

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

Now let's look at the prototype of the counter: (assuming the num.txt file exists)

<?php

$fp = fopen ("Num.txt", "R");

Open Num.txt File read-only mode

$num = Fgets ($fp, 5);

Read 4 digits

$num;

Browse Times plus One

Fclose ($FP);

Close 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.