<?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表示删除计数值文件;