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 the website as a statistical report. However, you always feel that the website is authentic. In, the visitor counter process is as follows 1. the first user else counter

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 the website as a statistical report. However, you always feel that the website is authentic.


In, the visitor counter process is as follows:

1. the first user browses a page.
2. the number of times the server program reads the page from the database or file.
3. store the number of times and send it back to the first user.
4. the second user browsed a page.
5. the number of times the server program reads the page from the database or file.
6. store the number of times and send it back to the second user.
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 modified by the author.




Visitor counter prototype


/*
Simple 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". "$ num". "bit boring ";
Exec ("rm-rf $ counterFile ");
Exec ("echo $ num> $ counterFile ");
}

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

DisplayCounter ($ counterFile );

?>





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 store the numbers in the $ num variable. Before the $ num variable appears in the browser, there is also a plus action to allow users to increase. Of course, if you want to fill the water, add two or three when adding one, 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 organize the counter into a function, which makes it much easier to use the sauce.

First, 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 add 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.../include of the server, and save the files
Counter. inc

The following is the MyCounter () function. Variables in the program for ease of understanding
$ CounterFile, $ fp, and $ num remain the same as the variable function in the counter set by David W. Bettis.


File ://---------------------------
// Visitor counter function MyCounter ()
// Author: Wilson Peng
// Copy right (C) 1999
File ://---------------------------
Function MyCounter (){
$ CounterFile = "/tmp". $ GLOBALS ["PHP_SELF"];
If (! File_exists ($ counterFile )){
If (! File_exists (dirname ($ counterFile ))){
Mkdir (dirname ($ counterFile), 0700 );
}
Exec ("echo 0> $ counterFile ");
}
$ Fp = fopen ($ counterFile, "rw ");
$ Num = fgets ($ fp, 5 );
$ Num + = 1;
Print "$ num ";
Echo $ counterFile;
Exec ("rm-rf $ counterFile ");
Exec ("echo $ num> $ counterFile ");
}
?>



Copyright? 1999, Wilson Peng

Of course, you can use the MyCounter () function embedded in the Homepage.


Require ("counter. inc ");
?>


Visitor counter Final version


You are Visitor



Copyright? 1999, Wilson Peng

To use this MyCounter () function, first add the require () function at the beginning of the Homepage, and introduce the MyCounter () function to become a part of the Homepage. Then Put the string where the counter is needed.

Function MyCounter (){
:
:

}
The preceding format must be used to create a function. Add the function string before the custom function name.

The Homepage useful to MyCounter () on each page will be added to the page path after/tmp, which can be achieved using the $ PHP_SELF variable.

$ CounterFile = "/tmp". $ GLOBALS ["PHP_SELF"];

Of course, you can change/tmp to another directory. otherwise, on a server such as SUN, if everything in reboot and/tmp is gone, you have to start counting again. If you do not know what directory you want to use, we recommend that you use the/var/log/counter Directory, together with other log and other changes.

If (! File_exists ($ counterFile )){
If (! File_exists (dirname ($ counterFile ))){
Mkdir (dirname ($ counterFile), 0700 );
}
Exec ("echo 0> $ counterFile ");
}



These five elements mainly check whether $ counterFile exists. if the file does not exist, check whether the directory exists and decide whether to create a directory. Create an archive and write it to 0.

$ Fp = fopen ($ counterFile, "rw ");
$ Num = fgets ($ fp, 5 );
$ Num + = 1;
Print "$ num ";
Echo $ counterFile;



These five elements are the files stored in the counter and the accumulated results are sent to the browser.

Exec ("rm-rf $ counterFile ");
Exec ("echo $ num> $ counterFile ");



Delete the counter file and create another one. This file-based Pure text counter is completed.

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.