Home >Backend Development >PHP Tutorial >Using js to call php content in HTML - counter example_PHP tutorial
We often see that the statistical code of many websites is displayed in the form of js calls. Let’s take a look at how to use it in practice.
$countfile = "num.txt";
//The file to define the counter is count.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 after adding 1
fclose ($fp);
//And close the file
echo 'document.write("'.$count.'")n';
//Output data in java script form
}
?>
js calls the method in the html file.