In PHP we sometimes write a simple website page access to the statistics, the following small series to introduce the use of PHP to implement the counter code, I hope this method is helpful to everyone.
Let's add a counter to the home page. Helps demonstrate how to read and write files and create your own functions. Counter.inc contains the following code:
| The code is as follows |
Copy Code |
< p>!--?
/*
| | A simple counter
*/
Function Get_hitcount ($counter _file)
{
/* Reset counter to zero
so if the counter is not yet in use, the initial value will be 1 you can, of course, set the initial value to 20000来.
*/
$count = 0;
//If the store counter file already exists, read the contents of
if (file_exists ($counter _file))
{
$fp =fopen ($counter _file, "R");
//We only take the top 20, I hope your site is not too popular AH
$count =0 fgets ($fp, 20);
//Because the function fgets () returns a string, we can automatically convert it to an integer
fclose ($fp) by adding a 0 method;
//File operation completed
}
//Add a count of values
$count;
//write new count value to file
$fp =fopen ($counter _file, "w");
Fputs ($fp, $count);
Fclose ($FP);
# Returns the Count value
return ($count);
}
?
Then we change the Front.php3 file to display this counter:
!--?
include ("Include/counter.inc");
I put the value in the file Counter.txt, read out and output
printf (" D
N",
Get_hitcount ("Counter.txt") );
include ("Include/footer.inc");
?
|
Example 2
| The code is as follows |
Copy Code |
|
1) Text counter $countfile = "/count.txt"; Set up files to save data
if (!file_exists ($countfile)) {//Determine if the file exists
EXEC ("Echo 0 > $countfile");
}
$fp = fopen ($countfile, "RW");
$length =filesize ($countfile);
$num = Fgets ($fp, $length);
$num + = 1;
EXEC ("Rm-rf $countfile");
EXEC ("echo $num > $countfile");
PRint "Traffic Totals:". " $num "." "; Show access times
?>
2) Graphics counter $countfile = "/count-num.txt"; Set up files to save data
if (!file_exists ($countfile))//Determine if the file exists
{EXEC ("echo 0 > $countfile");}
$fp = fopen ($countfile, "RW");
$length =filesize ($countfile);
$num = Fgets ($fp, $length);
$num + = 1;
EXEC ("Rm-rf $countfile");
EXEC ("echo $num > $countfile");
$len _str = strlen ($num);
for ($i =0; $i < $len _str; $i + +) {
$each _num = substr ($num, $i, 1);
$out _str = $out _str. "";
}
Print "Traffic totals:". " $out _str "." "; Show access times
|
http://www.bkjia.com/PHPjc/632702.html www.bkjia.com true http://www.bkjia.com/PHPjc/632702.html techarticle in PHP We sometimes write a simple website page access to the statistics, the following small series to introduce the use of PHP to implement the counter code, I hope this method is helpful to everyone. Let's ...