SoFunction
Updated on 2025-03-10

PHP uses functions to embed website visit counters

This is a way to implement a counter. If you want to see another method, please click:【PHP】Simple website visit counter implementation

If you want to see the specific code ideas, please click the link above.

Create Embed-Count folder

Create a file under the Embed-Count folder, with the following content:

<?php
function counter(){
  $counter = 0;               //Initialize variable  $max_len = 8;
  $lj = explode("/",$_SERVER["PHP_SELF"]); 

  //The hyperglobal variable $_SERVER['PHP_SELF'] saves the name of the currently running script Embed_Count/al_Embed_Fn.php
  $CounterFile="./counter/".$lj[count ($lj)-1].".dat";
  if(!file_exists($CounterFile)){
    if(!file_exists(dirname($CounterFile))){
      mkdir(dirname($CounterFile),0777);
    }
    $cf = fopen($CounterFile,'w');
    fputs($cf,'0');
    fclose($cf);
  }
  else{
    $cf = fopen($CounterFile,'r');
    $counter = trim(fgets($cf,$max_len));
    fclose($cf);
  }
  $counter++;
  $cf = fopen($CounterFile,'w');
  fputs($cf,$counter);
  fclose($cf);
  echo $counter;
}



?>

Create the al_Embed_Fn.php file under the Embed-Count folder, with the following content:

<?php
  include "";
?>
<html>
<head>
  <meta charset="UTF-8">
  <title>Embedded web counter-Liu Jiachen</title>
</head>
<body>
  <div >
    <span>Welcome!</span>
    <span>You are the first in this website<?php counter(); ?>A visitor</span>
  </div>
</body>
</html>

OK, after typing, do you find that you just encapsulate the code into a function?

That's right, but this time I used a lot of new functions and tips. Let me give you one by one.

Tips

1. Most php programmers are used to name the file extension of include or require "inc";

2.$CounterFile="./counter/".$lj[count ($lj)-1].".dat"; Position the counter file in the subfolder counter in the folder where the current script is located. The file is named after the current script name plus "dat", that is, al_Embed_Fn.

3. <?php include "" ?>Embed the counter function into the web page, and the script should be placed before the <HTML> tag; save it in the same folder as the web page, otherwise the file storage path should be specified in include

4.<?php counter(); ?> calls the counter() function, which returns the value of the counter
OK, the embedded version of this function call is also done.

Here are a few functions to talk about.

mkdir(dirname($CounterFile),0777): Create a directory named after the value of $CounterFlile, that is, ./counter. The access permission of the directory is the highest permission (readable, writable, executable);

dirname($CounterFile): Returns the directory part in the path

exploit('/',$_SERVER[PHP_SELF]): Returns an array of strings, each element is a substring cut by "/" as a boundary.

count($lj): count($lj): count the number of elements in the array &lj

Looking forward to my next version?

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.