插件29:点击计数器

<?php // Plug-in 29: Hit Counter

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

PIPHP_HitCounter("testcounter", "reset");

for ($j = 0 ; $j < 5 ; ++$j)
   PIPHP_HitCounter("testcounter", "add");

$result = PIPHP_HitCounter("testcounter", "get");
echo "Raw: $result[0] / Unique: $result[1]<br />";

PIPHP_HitCounter("testcounter", "delete");

function PIPHP_HitCounter($filename, $action)
{
   // Plug-in 29: Hit Counter
   //
   // This plug-in accepts the filename of a counter which is
   // incremented or read back. optionally a folder and init-
   // ialization number can be passed. This plug-in returns
   // an array. If $action is "get" the 1st and 2nd elements
   // contain the raw and unique hit counts. Otherwise the
   // return value is indeterminate. The arguments required
   // are:
   //
   //    $filename: Path/file name to save the counter details
   //               Must be unique to each counter.
   //    $action:   "reset"  = reset counts,
   //               "add"    = increment counts
   //               "get"    = return counts
   //               "delete" = delete counter

   $data = getenv("REMOTE_ADDR") .
           getenv("HTTP_USER_AGENT") . "\n";
   
   switch ($action)
   {
      case "reset":
         $fp = fopen($filename, "w");
         if (flock($fp, LOCK_EX))
            ;
         flock($fp, LOCK_UN);
         fclose($fp);
         return;

      case "add":
         $fp = fopen($filename, "a+");
         if (flock($fp, LOCK_EX))
            fwrite($fp, $data);
         flock($fp, LOCK_UN);
         fclose($fp);
         return;

      case "get":
         $fp = fopen($filename, "r");
         if (flock($fp, LOCK_EX))
            $file = fread($fp, filesize($filename) - 1);
         flock($fp, LOCK_UN);
         fclose($fp);
         $lines  = explode("\n", $file);
         $raw    = count($lines);
         $unique = count(array_unique($lines));
         return array($raw, $unique);

      case "delete":
         unlink($filename);
         return;
   }
}

?>


本插件接受一个保存统计信息的文件名和相关的操作信息,具体是:
$filename 保存计数值的文件名或路径
$action 对计数值采取的操作:
                reset表示复位全部计数器;
               add把当前访问添加到计数值;
               get表示读取点击数据;
               delete表示删除计数值文件;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值