菜单

PHP embeds website access counter with function

2017年11月23日 - PHPer

Create Embed-Count folder

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

<? PHP

Function counter () {

$counter = 0; / / initialize variables

$max_len = 8;

$lj = explode (“/”, “$_SERVER[“, “PHP_SELF”);

/ / superglobal $_SERVER[‘PHP_SELF’] save the script name 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;

}

&gt?;

Create the al_Embed_Fn.php file under the Embed-Count folder, which reads as follows:

<? PHP

Include “counter.inc.php””;

&gt?;

< html>

< head>

< meta charset= “UTF-8” >

< title> Embedded Web Counter – Liu Jiachen < /title>

< /head>

< body>

< div id= “DD” >

< span> welcome! < /span>

< span> you are the first &lt of this website; PHP counter ();? > bit visitor < /span>

< /div>

< /body>

< /html>

Okay, when the typing is finished, does the discovery just encapsulate the code into a function?

Yes, but this time it uses a lot of new functions and tricks. Let me give you one by one.

antic

1. most PHP programmers are used to the file extension of include or require named “Inc””;

2.$CounterFile= “./counter/”.$lj[count ($lj) -1]. “.Dat”; the counter file is positioned in the current script folder under the folder counter inside, the file to the current script name plus “dat” name, namely al_Embed_Fn.php.dat

3.<? PHP include “counter.inc.php””

> insert the counter function into the web page; the script should be placed before < HTML> before the tag; counter.inc.php is stored in the same folder as the web page; otherwise, the include is stored in the same folder as the web page

Specify the storage path of the file

4.<? PHP counter ();? > calls the counter () function, which returns the value of the counter

Well, the embedded function call is also done.

Here are a few functions that need to be said.

MKDIR (dirname ($CounterFile), 0777): to establish the value of $CounterFlile in the name of the directory,./counter directory access is the highest authority (read and write executable);

Dirname ($CounterFile): the return path of the directory Part

(explode ‘, $_SERVER[PHP_SELF]): returns an array of strings, each element is $_SERVER[PHP_SELF] by “/” as boundary cutting out the string

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

Looking forward to my next version?

发表评论

电子邮件地址不会被公开。