This article illustrates the method of implementing the counter in PHP. Share to everyone for your reference. Specifically as follows:
This collection of three PHP counter code, the three of them have a common point is all do not need a database, but the use of text files to achieve web browsing count.
The first PHP counter code is as follows:
Copy Code code as follows:
<?php
Counter
function Countx ($file = "Count.dat") {
if (file_exists ($file)) {
$FP =fopen ($file, "R");
$numx =fgets ($FP, 10);
Fclose ($FP);
$numx + +;
The above four lines of code can be replaced by an expression: $numx =file_get_contents ($file) +1;
}
else{
$NUMX = 1;}
File_put_contents ($file, $NUMX);//When the file does not exist, this function automatically creates the file and automatically turns the argument into a string write.
Echo $numx;
/* The entire function body can be replaced by two expressions: File_exists ($file)? file_put_contents ($file, file_get_contents ($file) +1): File_put_contents ($ File, "1"); ReadFile ($file);
*/
}
Function call
Countx ();
?>
The second PHP counter, the code is as follows:
Copy Code code as follows:
<?php
$counterfile = "Balong.txt";//file name of the value stored several paths
function Displaycounter ($counterfile) {
$fp = fopen ($counterfile, "RW");
$num = Fgets ($fp, 5);
$num + 1;
Print "You are the first." $num "." A guy looking at the Bahan counter;
EXEC ("Rm-rf $counterfile");
EXEC ("echo $num > $counterfile");
}
if (!file_exists ($counterfile)) {
EXEC ("Echo 0 > $counterfile");
}
Displaycounter ($counterfile);
?>
The third PHP counter code is as follows:
Copy Code code as follows:
<?php
$counterfile = "Www.jb51.net.txt";//file name of the value stored several paths
function Displaycounter ($counterfile) {
$fp = fopen ($counterfile, "RW");
$num = Fgets ($fp, 5);
$num + 1;
Print "You are the first." $num "." A guy looking at the Bahan counter;
EXEC ("Rm-rf $counterfile");
EXEC ("echo $num > $counterfile");
}
if (!file_exists ($counterfile)) {
EXEC ("Echo 0 > $counterfile");
}
Displaycounter ($counterfile);
?>
I hope this article will help you with your PHP program design.