Principles of PHP web page counters

Source: Internet
Author: User
Visitor counters are the most direct way for Web visitors to know the popularity index of the webpage or website. Especially for those who want to make money on the web page, the number of visitors is the best comment for advertisers. Of course, you can write the number of visitors to a website as a statistical report, but always the visitor counter is the most direct way for Web visitors to know the popularity index of the webpage or website. Especially for those who want to make money on the web page, the number of visitors is the best comment for advertisers. Of course, you can write the number of visitors to the website as a statistical report, but you always feel that you can see the truth directly.


In, the visitor counter process is as follows:

1. the first application browsed a page.
2. the number of times the server program reads this page from the material library or file.
3. store the number of times and send it back to the first application.
4. the second application browsed a page.
5. the number of times the server program reads this page from the material library or file.
6. store the number of times and send it back to the second application.
PHP does not have a special visitor counter function, but we can use the powerful function of PHP to write a visitor counter function.

The following form is a prototype of the visitor counter provided by David W. Bettis, which has been slightly corrected by the author.




Visitor counter prototype


/*
* ** Access counter for php3
(C) 1998 David W. Bettis
Dbettis@eyeintegrated.com
Medify by Wilson Peng
*/

$ CounterFile = '/tmp/counter.txt ';

Function displayCounter ($ counterFile ){
$ Fp = fopen ($ counterFile, 'rw ');
$ Num = fgets ($ fp, 5 );
$ Num = 1;
Print 'You are the nth '.' $ num'. 'bit boring part ';
Exec ('rm-rf $ counterfile ');
Exec ('echo $ num> $ counterFile ');
}

If (! File_exists ($ counterFile )){
Exec ('echo 0> $ counterfile ');
}

DisplayCounter ($ counterFile );

?>




Copyright? 1998 David W. Bettis

When reading this page, the PHP program first looks for the existence of the/tmp/counter.txt file. if it does not exist, it creates a counter.txt file and writes 0 to the file. Read the content of the counter.txt file, that is, the text file, and then store the numbers in the $ num variable. Before the $ num variable is displayed in the browser, there is also a plus action to allow the application to increase. Of course, if you want to inject water, add two or three when adding one action, but self-deception is useless. Finally, the visitor count is stored back to/tmp/counter.txt.
Everything is OK.

Of course, every page must be written in this way, so it is difficult. At this time, we can use the require () function provided by PHP to pack the counter into a form, which is much more convenient for the application of the sauce.

First, you must add the Apache configuration file (httpd. conf) to the PHP include file path. For example, to set all include files to be in the http://abcdefghijk.com.tw/include, you can participate in the following example in httpd. conf

Php3_include_path.:./include:./include

Do not forget to restart the Apache server. The new include path is valid.

./Apachectl restart

Then put the following files in the directory of.../include on the server and save the files
Counter. inc

The following is the MyCounter () function. Variables in the program for ease of understanding

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.