PHP Embedding Web Visit Counters with Functions

  • 2021-08-12 02:14:01
  • OfStack

This is one way to implement the 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 on the link above.

Create Embed-Count folder

Create the counter. inc. php file under the Embed-Count folder as follows:


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

  // 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 as follows:


<?php
  include "counter.inc.php";
?>
<html>
<head>
  <meta charset="UTF-8">
  <title> Embedded web page counter - Liu Jiachen </title>
</head>
<body>
  <div id="dd">
    <span> Welcome! </span>
    <span> You are the first member of this website <?php counter(); ?> Visitors </span>
  </div>
</body>
</html>

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

Yes, but this time I used a lot of new functions and tricks. Let me give you 1 1.

Tips

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

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

3. < ?php include "counter.inc.php" ? > To embed the counter function into the web page, this script should be placed in the < HTML > Before marking; counter. inc. php is saved in the same folder as the web page, otherwise, the file storage path should be indicated in include

4. < ?php counter(); ? > Call the counter () function, which returns the value of the counter
Ok, the embedding of this function call is also done.

Here are a few functions that need to be said 1.

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

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

explode ('/', $_SERVER [PHP_SELF]): Returns an array of strings, each element being a substring of $_SERVER [PHP_SELF] cut with "/" as the boundary

count ($lj): Statistical Array & Number of elements in lj

Looking forward to my next version?


Related articles: