PHP Implementation Counter Method summary _php Skill

Source: Internet
Author: User

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.

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.