현재 뷰의 수를 계산하고 .txt 파일에 저장하는 작업 스크립트가 있습니다.페이지 방문 카운터 - 작동하지만 IP 주소별로 제한하고 싶습니다.
잘 작동하지만 IP 주소를 어떻게 제한합니까?
나는 이것을 시도했지만 그것은 중요하지 않다.
// Get filename of Page
$pageName = basename($_SERVER["SCRIPT_FILENAME"], '.php');
$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
// Remove .php extension
$counterName = basename($pageName, ".php").".txt";
// Open the file for reading
// "a+" Read & write the file. Create file if not exist.
$fp = fopen($counterName, "a+");
$fpIP = fopen("ip_".$counterName, "a+");
fwrite($fpIP, $ip."-");
// Get the existing count
$count = fread($fp, 1024);
// Close the file
fclose($fp);
// Add 1 to the existing count
$count = $count + 1;
// Reopen the file and erase the contents
$fp = fopen($counterName, "w");
$ipRead = file_get_contents('ip_index.txt');
if(strpos($ipRead, "$ip") !== FALSE) {
echo $count;
}
else {
fwrite($fp1, $count);
echo $count;
}
fclose($fp);
다음은 그들이 자신의 IP 주소를 기반으로 페이지에왔다 얼마나 많은 시간을 각 개별 방문자를 표시합니다 Barmar의 코드 (완전 작업) 내 업데이트 된 코드입니다.
// Get filename of Page
$pageName = basename($_SERVER["SCRIPT_FILENAME"], '.php');
// Remove .php extension
$counterName = basename($pageName, ".php").".counter";
// Get IP
$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
$count_text = @file_get_contents($counterName);
$counters = $count_text ? json_decode($count_text, true) : array();
if (isset($counters[$ip])) {
$counters[$ip]++;
} else {
$counters[$ip] = 1;
}
file_put_contents($counterName, json_encode($counters));
echo $counters[$ip];
파일에 개수를 저장하는 특별한 이유가 있습니까? 또한 $ ip를 echo하는 경우 값은 무엇입니까? – kyle
IP를 파일 이름에 넣거나 IP가 키로 연결된 연관 배열을 파일에 넣어야합니다. – Barmar