Home > Backend Development > PHP Tutorial > PHP simple text counter_PHP tutorial

PHP simple text counter_PHP tutorial

WBOY
Release: 2016-07-13 10:34:13
Original
1282 people have browsed it

Use the counter code directly in the php file:

 $countfile = "num.txt";

//The file written by the definition counter is num.txt in the current directory, and then we should test whether the file can be opened

 if (($fp = fopen($countfile, "r+")) == false) { //Open the file in read-write mode, exit if it cannot be opened

printf ("Failed to open file %s!",$countfile);

exit;

 }

else

 {

//If the file can be opened normally, read the data in the file, assuming it is 1

 $count = fread ($fp,10);

//Read 10-bit data

 $count = $count + 1;

 fclose ($fp);

//Close the current file

 $fp = fopen($countfile, "w+");

//Open the file in overwrite mode

 fwrite ($fp,$count);

//Write new data plus 1

 fclose ($fp);

//And close the file

echo 'Hello, you are the '.$count.' visitor';

//Output data

 }

 ?>

Just save the above content into php and create num.txt in the same directory

But we often see that the statistical codes of many websites are displayed in the form of js calls. Let’s briefly modify it:

 $countfile = "num.txt";

//The file written by the definition counter is num.txt in the current directory, and then we should test whether the file can be opened

 if (($fp = fopen($countfile, "r+")) == false) { //Open the file in read-write mode, exit if it cannot be opened

printf ("Failed to open file %s!",$countfile);

exit;

 }

else

 {

//If the file can be opened normally, read the data in the file, assuming it is 1

 $count = fread ($fp,10);

//Read 10-bit data

 $count = $count + 1;

 fclose ($fp);

//Close the current file

 $fp = fopen($countfile, "w+");

//Open the file in overwrite mode

 fwrite ($fp,$count);

//Write new data plus 1

 fclose ($fp);

//And close the file

echo 'document.write("'.$count.'")';

//Output data in javascript form

 }

 ?>

Save the above content to FileCount.php, and create num.txt in the same directory.

In the html file, js calls the method.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752063.htmlTechArticleUse the counter code directly in the php file: $countfile = num.txt; //The file that defines the counter is written to num.txt in the current directory, then we should test whether the file can be opened if (($fp...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template