The counter is the most direct way for Web visitors to know the popularity index of the webpage or website. at the same time, the number of visitors is also the best basis for advertisers to advertise. Although many websites provide free counters, after all, they are not made by themselves. not counting is the most direct way for Web visitors to know the popularity index of the webpage or website. at the same time, the number of visitors is also the best basis for advertising to advertisers. Although many websites provide free counters, they are not made by themselves and cannot reflect their own characteristics. The following describes how to create a counter in the PHP environment.
The visitor counter process is as follows:
The first application browses a page.
The number of times that the server program reads this page from the material library or file.
Store the number of times and send it back to the first application.
The next application browses a page.
The number of times that the server program reads this page from the material library or file.
Store the number of times and send it back to the next application.
PHP does not have a direct counter function, but with its powerful function, we can easily write a counter by ourselves. The implementation of counters is divided into two stages: one is to implement counting, and the other is to display numbers. There are two ways to implement counting: one is the file-based counting method, and the other is the database-based counting method. There are also two ways to display numbers: one is normal text display, and the other is graphical display. We will introduce the four cases one by one.
I. counting implementation
1. File-Based Counting
Principle: record the number of previous visitors in a text file. when a webpage is visited, open the file and read the previous number of visitors, add 1, get the latest number of visitors, and write the latest number of visits back to the file that records the number of visitors.
Implementation;
Program Clarification: when browsing this page, the PHP program first looks for the existence of the counter.txt file. if it does not exist, it creates a counter.txt file and writes 0 to the file. Then read the content of counter.txt, add the number to be read, and write it back to the file.
2. Database-based counting
Principle: record the number of visitors in a database. when the webpage is visited, read the previous number of visitors from the database and add 1 to get the latest number of visitors, write the latest visit data back to the database.
Implementation: assume that the database is a Mysql database named XinXiKu. First, create a data table count. The Table field contains only one counter field. the default value is 0 to record the number of visitors.
// Connect data
$ Db = mysql_connect ('localhost', 'root ','');
Mysql_select_db ('xinxiku', $ db );
// Update the number of visitors
$ Result = mysql_query ('update count set counter = counter 1', $ db );
?>
Program Clarification: When a visitor browses this page, he first connects to the database and uses the original value plus 1 to update the count field to increase the number of visits.
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