Using PHP to realize the counter is very simple, the implementation of the counter is divided into two stages: first, the implementation of the count, the second is to achieve the number of digital display, and in the implementation of the count of two ways: one is based on the count of the way the database, and the second is based on the In the implementation of the display of numbers there are two ways: one is the normal text display, and the second is the graphical display. Here are a few of these four situations:
First, counting
1. File-based Count
In this way, a file is used to hold the number of accesses, which requires a directory with a permission of at least 007, so that the httpd process can create and write files, and you can make a new subdirectory in your own directory, such as: Visit_log, change the permissions to 777. Here is the code that implements the file count:
?
if (!file_exists ("Visit_log/counter.txt")) {
$FP =fopen ("Visit_log/counter.txt", "w");
Fwrite ($fp, "000001"); This assumes a maximum of 6 digits and you can modify it as needed
$counter = "000001";
Fclose ($FP);
//Check if the Counter.txt file exists, create a new file if it does not exist, and write "00001".
else{
$FP =fopen ("Visit_log/counter.txt", "R");
$counter =fread ($FP, 6);
//-----------------------------
$counter +=1;
//-----------------------------
Fclose ($FP);
Switch (strlen ($counter)) {//counter variable format, so it looks beautiful
Case 1:
$counter = "00000". $counter;
Break
Case 2:
$counter = "0000". $counter;
Break
Case 3:
$counter = "$counter".
Break
Case 4:
$counter = "$counter";
Break
Case 5:
$counter = "0". $counter;
Break
}
$FP =fopen ("Visit_log/counter.txt", "w");
Fwrite ($fp, $counter);
Fclose ($FP);
}
?>
The above code is based on the file count, do not know if you have found a problem, that is, as long as someone visited the page, whether he is refreshed or from the other pages of your site to jump to the page, you will add a value of 1, so that the home count has lost authenticity. So what's going on here? In fact, there is a very simple way to return to the homepage of the link to add a parameter visited=1, such as: Index.php?visited=1, before the count first check whether the visited variable has been assigned, if not assigned, the counter plus 1. Of course, we need to revise the code labeled "---------------" above:
if (empty ($visited)) {
$counter +=1;
}
2. Database-based Count
The database I chose here is MySQL. We first want to build a database Visit_log, and then build a table in the database Visit_counter, in order to achieve the basic counting function, only need a field to amount. The operation is as follows:
(1) Establishment of database Visit_log
Mysqladmin Create Visit_log–u Root–p
You can create a Visit_log database by typing the root password correctly.
(2) Set up table Visit_counter
MySQL Visit_log–u root–p
Mysql>create table Visit_counter (amount int (6));
Mysql>insert into Visit_counter (amount) values (0);
The following is the code that implements the data count:
?
$CN =mysql_connect ("localhost", "root", "Hu Jintao");
mysql_select_db ("Visit_log", $CN);
$sql = "SELECT * from Visit_counter";
$result =mysql_query ($sql, $CN);
$record =mysql_fetch_array ($result);
if (empty ($visited)) {
$counter = $record ["Amount"]+1;
$sql = "Update visit_counter set amount= $counter";
$result =mysql_query ($sql, $CN);
}
Mysql_close ($CN);
Switch (strlen ($counter)) {//Format counter variable
Case 1:
$counter = "00000". $counter;
Break
Case 2:
$counter = "0000". $counter;
Break
Case 3:
$counter = "$counter".
Break
Case 4:
$counter = "$counter";
Break
Case 5:
$counter = "0". $counter;
Break
}?>
The above code I will not say more, are the basic operation of MySQL, it is not to the site of the "reference manual" to download some information about MySQL.
The above two methods to achieve the counting function, careful netizen may see counter variable seems useless. In fact, it is the variable we use to display.
Second, the display of the count
1. Text display
The text display is simple, just insert the following code where you want it to appear:
<?echo $counter;? >
Of course, you can add <font color> change its color and so on. One of the advantages of text display is to speed up the download time, the disadvantage is not lively.
2. Graphic display
For a graphic to display, the above code cannot be put together with a file containing HTML encoding, when we can build another file called showcounter.php, with the following code:
?
$CN =mysql_connect ("localhost", "root", "Hu Jintao");
mysql_select_db ("Visit_log", $CN);
$sql = "SELECT * from Visit_counter";
$result =mysql_query ($sql, $CN);
$record =mysql_fetch_array ($result);
if (empty ($visited)) {
$counter = $record ["Amount"]+1;
$sql = "Update visit_counter set amount= $counter";
$result =mysql_query ($sql, $CN);
}
Mysql_close ($CN);
Switch (strlen ($counter)) {//Format counter variable
Case 1:
$counter = "00000". $counter;
Break
Case 2:
$counter = "0000". $counter;
Break
Case 3:
$counter = "$counter".
Break
Case 4:
$counter = "$counter";
Break
Case 5:
$counter = "0". $counter;
Break
}
Header ("Content-type:image/gif");
$pic =imagecreate (50,17); Creating images
$bkcolor =imagecolorallocate ($pic, 0,0,0); Define background color
$fcolor =imagecolorallocate ($pic, 0,255,0); Define Font Color
Imageline ($pic, 0,0,50,17, $bkcolor);
Imagestring ($pic, 3,1,1, $counter, $fcolor);
Imagegif ($pic);
Imagedestroy ($pic);
?>
Next, insert in the place where you want to use the counter: . Note that you need to install the GD library in your PHP to create graphics.
Using the database you can write a powerful counting system. For example, record the number of visits per day, record each user's visit, and so on.
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.