SoFunction
Updated on 2025-04-13

Use PHP to write counters (detailed introduction)

PHP instance analysis: Counter
Author: Sucre_tiger
This counter uses text counting, but does not use the database. The following functions can be implemented:
Using a text file to count multiple pages to reduce the I/O occupancy of the server. In the file that needs to be recorded, only a few lines of code are required.
The basic ideas are as follows:
The server program reads the number of times the page is browsed from a text file (because when all files make requests to the server, their environment variable REQUEST_URI represents where it comes from..., so, use the environment variable REQUEST_URI of the requested file to distinguish which page is browsing.), adds this number of times and displays it on the computer of the user who calls this page.
Please first look at the data sample recorded in my data text (red is the number of views, and the previous one is the corresponding file being browsed)
/script/|3|/script/|11| /script/|5| /testhtml/|7|/testhtml/|6|
Now,Let's go!

Copy the codeThe code is as follows:

<html><head>
<title>Counter</title>
<head>
<body>
<?php
/* Define the text file that stores data */
$counterFile="\";
/* Define a mark to identify whether the data to be recorded now has been in text data */
$sign=False;
/* Read data into the variable $sunterData alternate, */
$counterData=file($counterFile);
/* Use count() function to calculate how many records there are in total */
/* Use the exploit() function to separate $counterData[$i] by symbol\"|\" and send it back to the variable $varArray in an array */
/* The function implode() is just the opposite of exploit(), and the array $varArray is connected with the symbol\"|\" and sent back to $counterData */
/* also utilizes the environment variable $PATH_INFO
for($i=0;$i<=count($counterData)-1;$i++)
 {
$varArray=explode(\"|\",$counterData[$i]);
if ($varArray[0]==$GLOBALS[\"REQUEST_URI\"])
 {
$varArray[1]++;
print($varArray[1]);
$counterData[$i]=implode(\"|\", $varArray);
$sign=True;
/* After finding the location of this record, use break to exit the loop */
break;
 }
 }
/* Here, use the function of the implode() function to organize the data and write it to the text file together */
/* In this way, the I/O usage of the server will be reduced to the lowest point
$data=implode(\"\",$counterData);
/* Open the text file and write the data to */
$fp=fopen($counterFile,\"w\");
fputs($fp,$data);
/* If the data that needs to be recorded is not in the text, the flag $sign=Flase, then add data to the text */
if (!$sign) {fputs($fp,\"\\n\".$GLOBALS[\"REQUEST_URI\"].\"|\".\"1\".\"|\");
print(\"1\");
/* Close the data file */
fclose($fp);
?>
</body>
</html>

We have seen the working process of this program and have all known the idea, but if we write it like this in every file, wouldn’t it be too troublesome?
Don't panic! We also have the powerful require() function provided by PHP! We write it into a function, which makes it much easier to use. What are you waiting for? Here is the function you want:

Copy the codeThe code is as follows:

<?php
function Counter()
{
  $counterFile=\"/freespace/sucre/public_html/\";
  $counterData=file($counterFile);
  $sign=False;
  for($i=0;$i<=count($counterData)-1;$i++)
{
  $varArray=explode(\"|\",$counterData[$i]);
  if ($varArray[0]==$GLOBALS[\"REQUEST_URI\"])
{
$varArray[1]++;
print($varArray[1]);
$counterData[$i]=implode(\"|\", $varArray);
$sign=True; break;
}
}
  $data=implode(\"\",$counterData);
  $fp=fopen($counterFile,\"w\");
  fputs($fp,$data);
  if (!$sign)
{
  fputs($fp,\"\\n\".$GLOBALS[\"REQUEST_URI\"].\"|\".\"1\".\"|\");
  print(\"1\");
}
fclose($fp);
}
?>
The best way to test is "practice". Let's see how we call it. Let's first look at an example:

<?php
require(\"\");
?>
<html>
<head>
<title> Web Counter Final Edition </title>
</head>
<body>
You are the number <? counter();?> reader
</body>
</html>

You just need to add the require() function to the file header of the HTML file you want to count and introduce the counter() function as part of the homepage. Just add <? counter();?> where needed.
There are a few more issues to note:
1. The file that records data must have "write" permission, generally set to "666". If the file is stored in a subdirectory, you must also have "write" permission to this "directory";
2. I encountered such a problem during the debugging process. I placed the sum under a subdirectory include, and then called it with the require() function under different subdirectories. The format is as follows: <?php
      require("../include/")
    ?>
However, there are always errors of "insufficient permissions". If you have any experts, please give me advice.