PHP Implementation Counter Method Summary, PHP counter summary _php Tutorial

Source: Internet
Author: User

PHP Implementation Counter Method Summary, PHP counter Summary


The example of PHP implementation counter is described in this paper. Share to everyone for your reference. Specific as follows:

This collection of three PHP counter code, all of them three have a common point is that 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 CodeThe code is 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 parameter 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 CodeThe code is as follows: <?php
$counterfile = "Balong.txt";//the file name of the stored value 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 CodeThe code is as follows: <?php
$counterfile = "Www.jb51.net.txt";//the file name of the stored value 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/937727.html www.bkjia.com true http://www.bkjia.com/PHPjc/937727.html techarticle PHP Implementation Counter method Summary, PHP counter Summary The example of this article describes the PHP implementation of the counter method. Share to everyone for your reference. Here are the following: three PHP counts are collected here ...

  • Related Article

    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.