The principle of PHP Web counter

Source: Internet
Author: User
Tags include log mkdir variables require
Counter | Web Visitor counter is the most direct way for a web visitor to know the popularity index of the page or website. Especially those who want to make money from the Web, the number of visitors is the best way to find advertisers. Of course, the number of visitors to the site written as a statistical report, but always feel directly to see more real, in the end seeing as a basis.


In the above illustration, the process of the visitor counter is as follows

1. The first user browses to a page.
2. Server program reads the number of times the page has been browsed from a database or file.
3. Add a storage number and return it to the first user.
4. Second user browses to a page.
5. Server program reads the number of times the page has been browsed from a database or file.
6. Add a storage to the number of times and send it back to the second user.
PHP has no special visitor counter function, but we can write a visitor counter function with PHP's powerful features.

The following function is the prototype of the visitor counter, which is provided by David W. Bettis and has been slightly modified by the author.


<title> Visitor Counter Prototypes </title>
<body>
<?php
/*
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 first." $num "." "a boring person";
EXEC ("Rm-rf $counterfile");
EXEC ("echo $num > $counterfile");
}

if (!file_exists ($counterfile)) {
EXEC ("Echo 0 > $counterfile");
}

Displaycounter ($counterfile);

?>
</body>


The Copyright? 1998 David W. Bettis

When reading this page, the PHP program first looks for the existence of the/tmp/counter.txt file and, if it does not exist, creates a counter.txt file and writes 0 to the file. Then read the contents of the Counter.txt file, that is, the text file, and then the number of the text into the $num variable. The $num variables appear in front of the browser, plus the action, so that users can increase. Of course, if you want to water, add two or three in addition to the action, but self-deception is useless. Finally, the number of visitors will be returned to save/tmp/counter.txt
It's all OK.

Of course, every page should be written like this, it is not trouble to the extreme. At this time, we can use PHP provided by the Require () function, the counter is organized into a function, the sauce in the use of a lot of convenience.

The first step is to add the Apache profile (httpd.conf) to the PHP include file path. For example, to set all the Include files 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

Again, in the server's .../include directory, put the following file, file name Save As
Counter.inc

The following is the MyCounter () function. To make it easier for readers to understand, variables in the program
$counterfile, $FP and $num remain the same as the variables in the counters set by David W. Bettis.


<?php
file://---------------------------
Visitor counter Function MyCounter ()
Author:wilson Peng
Copyright (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");
}
?>


The Copyright? 1999, Wilson Peng

Of course, to use the words to add homepage embedded in the MyCounter () function, you can use.


<?php
Require ("Counter.inc");
?>
<title> Visitor Counter Final edition </title>
<body>
You are the first;? MyCounter ();?> visitors
</body>


The 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 as part of the homepage. After that. MyCounter () The?> string is placed where you want the counter.

function MyCounter () {
:
:

}
When you create a function, you need to use the format above. Add a function string before the name of the custom function.

Every page that is useful to MyCounter () homepage will add the path to the page after/TMP, which can be achieved with $php_self variables.

$counterfile= "/tmp" $globals["php_self"];

Of course, if you want to change/tmp to another directory can also, otherwise in the SUN and other servers, if the reboot,/tmp things are gone, to start again count. If you do not know what directory to use, it is recommended to use the/var/log/counter directory, and other log data and other changes to put together.

if (!file_exists ($counterfile)) {
if (!file_exists (DirName ($counterfile)) {
mkdir (DirName ($counterfile), 0700);
}
EXEC ("Echo 0 > $counterfile");
}

This five elements is mainly to check whether the existence of $counterfile, if the file does not exist to see whether the directory exists, decided to create a directory. The file is then created and written to 0.

$fp = fopen ($counterfile, "RW");
$num = fgets ($fp,5);
$num + 1;
print "$num";
Echo $counterfile;

The five elements is to open the counter to store the file, and its cumulative results sent to the browser side.

EXEC ("Rm-rf $counterfile");
EXEC ("echo $num > $counterfile");

Finally, the counter file is deleted, and then a new one is established. Complete the file based on the text counter.

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.