Home  >  Article  >  Backend Development  >  PHP method of using text to count visits PHP timing method PHP encapsulation method PHP system authorizer

PHP method of using text to count visits PHP timing method PHP encapsulation method PHP system authorizer

WBOY
WBOYOriginal
2016-07-29 08:49:38891browse

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:

<&#63;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);
&#63;>

Usage:

<&#63;php
include("counter.php");
&#63;>

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.

Statement:
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