Home > Article > Backend Development > Talk about how to use PHP text counter remotely_PHP tutorial
We all must be aware of the powerful functions of PHP. You can easily write a text counter using it. However, this can only be used on sites where you can run PHP programs. How can you also use it on sites where you cannot run PHP? What about using it? Please read below:
In fact, this uses a JavaScript document.write function to display our count.
Insert an html language: where you need the counter. That's it. The following
is the source program of counter.php.
//---------------------------------------- ----------------------------
// Text counter
// Author: Xu Xiaodong (chinlone)
/ / Home page: http://lthx.126.com
// E-mail: chinlone@china.com
// Oicq:4703122
//----------- -------------------------------------------------- -------
$counterFile = "counter.txt";//File name
if (!file_exists($counterFile)) {
$fp = fopen($counterFile,"a" );//If there is no counter.txt file, create it
fwrite($fp,"0");//Write the 0 value back to the file
fclose($fp);//Close the file
}
$fp = fopen($counterFile,"r");//Open the counter.txt file in reading mode
$num = fgets($fp,9);//Read the file pointer pointed to The length of the line (first line) is 9, and the value is given to the num variable
fclose($fp); //Close the file
$num++; //The value is increased by 1