如何实现不同IP地址的浏览次数统计
<?php
// 访客计数器函数
function counter() {
!empty($_GET['weburl'])
[解决办法]
die('weburl不能为空');
$weburl = trim($_GET['weburl']);
$pattern = "/((25[0-5]
[解决办法]
2[0-4]\d
[解决办法]
1?\d?\d)\.){3}(25[0-5]
[解决办法]
2[0-4]\d
[解决办法]
1?\d?\d)/";
//对录入的ip格式进行判断
if(! preg_match($pattern,$weburl))
die('IP地址格式不正确;');
//使ip长度固定
$arr = explode('.',$weburl);
for($i=0;$i<4;$i++){
$arr[$i] = str_pad($arr[$i],3,' ',STR_PAD_LEFT);
}
$weburl = join('.',$arr);
$file = 'counter.txt';
if (! file_exists($file)) {
$num = 1;
$cf = fopen($file, 'w');
fwrite($cf, $weburl.'-'.$num);
fclose($cf);
return $num;
} else {
$cf = fopen($file, 'r+');
while(!feof ($cf)){
$str1 = fgets($cf);
$str2 = substr($str1,0,strpos($str1,'-'));
if($weburl == $str2){
$len = strlen($str1);
$num = (int) trim(substr($str1,strpos($str1,'-')+1));
$num++;
$str1 = $weburl.'-'.$num;
//如果存在则把指针返回到该行前,覆盖写入内容,实现修改
fseek($cf,-$len,SEEK_CUR);
fwrite($cf,$str1);
fseek($cf,$len,SEEK_CUR);
fclose($cf);
return $num;
}
}
//未找到则在末尾换行增加一行记录
fwrite($cf, "\r\n".$weburl.'-1');
return 1;
}
}
?>
<html>
<head>
<title>访客计数器</title>
</head>
<body>
<center>
<h1>欢迎访问</h1><br />
<form action="" name="url-form" method="get">
<div>
<input type="text" name="weburl" size="15" />
<input type="submit" name="Submit" value="提交" />
</div>
</form>
<hr />
<font size="7" color="red">
您是第<?php echo counter(); ?>位访客
</font>
</center>
</body>
</html>