PHP Simple Counter Instance Program _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.