How PHP uses functions to embed web site traffic counters

Source: Internet
Author: User
This article is mainly for you to introduce the PHP function embedded website access counter, with a certain reference value, interested in small partners can refer to

Create a Embed-count folder

Under the Embed-count folder, create a counter.inc.php file that reads as follows:

<?phpfunction counter () {  $counter = 0;               Initialize variable  $max _len = 8;  $LJ = Explode ("/", $_server["php_self"]);   Hyper global 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;}? >

Under the Embed-count folder, create a al_embed_fn.php file that reads as follows:

<?php  include "counter.inc.php";? >

OK, after typing done, is it just a function of encapsulating the code?

Yes, but this time we've used a lot of new functions and tricks. Let me give you one by one.

Little Tricks

1. Most PHP programmers are accustomed to the name "Inc" of the file extension of include or require;

2. $CounterFile = "./counter/". $lj [Count ($LJ)-1]. ". DAT "; Locate the counter file in the subfolder under the folder where the current script is located counter, the file with the current script name plus" dat "name, that is Al_Embed_Fn.php.dat

3.<?php include "counter.inc.php"?> to embed the counter function in a Web page that should precede the <HTML> tag Counter.inc.php is saved in the same folder as the Web page, otherwise the file's storage path is indicated in the Include

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

Here are a few functions to say.

mkdir (DirName ($CounterFile), 0777): establishes a directory with the value of $counterflile, that is, the./counter, the access permission of the directory is the highest (readable writable executable);

DirName ($CounterFile): Returns the directory portion of the path

Explode ('/', $_server[php_self]): Returns an array of strings, each of which is $_server[php_self] "/" as a substring of the boundary cut out

Count ($LJ): counts the number of elements in an array &lj

Looking forward to my next version?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.