The example in this article describes how PHP uses text to count visits. Share it with everyone for your reference, the details are as follows:
Method 1:
$fp = fopen("counter.txt", "r+"); while(!flock($fp, LOCK_EX)) { // acquire an exclusive lock // waiting to lock the file } $counter = intval(fread($fp, filesize("counter.txt"))); $counter++; ftruncate($fp, 0); // truncate file fwrite($fp, $counter); // set your data fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock fclose($fp);
Method 2:
counter.php file:
<?php /* counter */ //opens countlog.txt to read the number of hits $datei = fopen("countlog.txt","r"); $count = fgets($datei,1000); fclose($datei); $count=$count + 1 ; echo "$count" ; echo " hits" ; echo "\n" ; // opens countlog.txt to change new hit number $datei = fopen("countlog.txt","w"); fwrite($datei, $count); fclose($datei); ?>
Usage:
<?php include("counter.php"); ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP File Operations", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.