How to use text to count visits in PHP, _PHP tutorial

WBOY
Release: 2016-07-12 08:52:52
Original
765 people have browsed it

How PHP uses text to count visits,

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);

Copy after login

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;>

Copy after login

Usage:

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

Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125869.htmlTechArticleHow to use text to count visits in php. This article describes the method of using text to count visits in php. Share it with everyone for your reference, as follows: Method 1: $fp = fopen("c...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template