Using PHP to implement the counter is very simple, the implementation of the counter is divided into two stages: one is to achieve the count, the second is to realize the digital display, and in the implementation of the count, there are two ways: one is based on the file count, and the second is based on the database count method.
1. File-based Count
<? if (!file_exists ("Visit_log/counter.txt")) { $fp =fopen ("Visit_log/counter.txt", "w"); Fwrite ($fp, "000001"); Assuming a maximum of 6 bits, you can modify $counter = "000001" as needed; Fclose ($FP); } Check to see 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 is formatted so that it looks beautiful case 1: $counter = "00000". $counter; break; Case 2: $counter = "0000". $counter; break; Case 3: $counter = "$". $counter; break; Case 4: $counter = "xx". $counter; break; Case 5: $counter = "0". $counter; break; } $FP =fopen ("Visit_log/counter.txt", "w"); Fwrite ($fp, $counter); Fclose ($FP); } ? >
2. Database-based Count
<? $CN =mysql_connect ("localhost", "root", "******"); 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)) {//counter variable is formatted case 1: $counter = "00000". $counter; break; Case 2: $counter = "0000". $counter; break; Case 3: $counter = "$". $counter; break; Case 4: $counter = "xx". $counter; break; Case 5: $counter = "0". $counter; break; }? >
Summarize:
The above in two ways to achieve the Count function, careful netizen may see counter variable seems useless. It is actually the variable we use to display it.
Related recommendations:
PHP Counter Program
PHP counter Program: Solve all possible problems _php tutorial
PHP Counter Program _php Basics