The example in this article describes how PHP uses text to count visits. Share it with everyone for your reference, 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"); ?>
Interested in more PHP related content Readers of this website 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 Syntax", "Summary of PHP Operation Office Document Skills" (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "A Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
The above introduces the method of using text to count visits in PHP, including the methods of PHP. I hope it will be helpful to friends who are interested in PHP tutorials.