首页 > 开发 > PHP > 正文

PHP用函数嵌入网站访问量计数器

2024-05-04 22:02:22
字体:
来源:转载
供稿:网友

是一种方法实现计数器。想看另一种方法的请点击:【PHP】简单的网站访问量计数器实现

想看具体代码思路的也请点击上面的链接。

创建Embed-Count文件夹

在Embed-Count文件夹下面创建counter.inc.php文件,内容如下:

<?phpfunction counter(){  $counter = 0;               //初始化变量  $max_len = 8;  $lj = explode("/",$_SERVER["PHP_SELF"]);   //超全局变量$_SERVER['PHP_SELF']保存了当前运行脚本的名字 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;}?>

在Embed-Count文件夹下面创建al_Embed_Fn.php文件,内容如下:

<?php  include "counter.inc.php";?><html><head>  <meta charset="UTF-8">  <title>嵌入式网页计数器-刘佳晨</title></head><body>  <div id="dd">    <span>欢迎您!</span>    <span>您是本网站的第<?php counter(); ?>位访客</span>  </div></body></html>
欢迎您! 您是本网站的第位访客
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表