我将屏蔽除大型搜索引擎之外的所有机器人。我的阻塞方法之一是检查"language":Accept-Language:如果它没有Accept-Language,那么机器人的IP地址将被阻塞,直到2037年。Googlebot没有Accept-Language,我想用DNS查找来验证它
<?php
gethostbyaddr($_SERVER['REMOTE_ADDR']);
?>可以使用gethostbyaddr吗,有人可以通过我的"gethostbyaddr保护“吗?
发布于 2010-06-20 09:37:39
How to verify Googlebot。
发布于 2019-10-24 13:53:50
function detectSearchBot($ip, $agent, &$hostname)
{
$hostname = $ip;
// check HTTP_USER_AGENT what not to touch gethostbyaddr in vain
if (preg_match('/(?:google|yandex)bot/iu', $agent)) {
// success - return host, fail - return ip or false
$hostname = gethostbyaddr($ip);
// https://support.google.com/webmasters/answer/80553
if ($hostname !== false && $hostname != $ip) {
// detect google and yandex search bots
if (preg_match('/\.((?:google(?:bot)?|yandex)\.(?:com|ru))$/iu', $hostname)) {
// success - return ip, fail - return hostname
$ip = gethostbyname($hostname);
if ($ip != $hostname) {
return true;
}
}
}
}
return false;
}在我的项目中,我使用这个函数来识别Google和Yandex搜索机器人。
detectSearchBot函数的结果是缓存。
该算法基于谷歌的推荐-- https://support.google.com/webmasters/answer/80553
发布于 2016-05-23 17:06:11
除了Cristian的回答之外:
function is_valid_google_ip($ip) {
$hostname = gethostbyaddr($ip); //"crawl-66-249-66-1.googlebot.com"
return preg_match('/\.googlebot|google\.com$/i', $hostname);
}
function is_valid_google_request($ip=null,$agent=null){
if(is_null($ip)){
$ip=$_SERVER['REMOTE_ADDR'];
}
if(is_null($agent)){
$agent=$_SERVER['HTTP_USER_AGENT'];
}
$is_valid_request=false;
if (strpos($agent, 'Google')!==false && is_valid_google_ip($ip)){
$is_valid_request=true;
}
return $is_valid_request;
}便笺
有时在使用$_SERVER['HTTP_X_FORWARDED_FOR']或$_SERVER['REMOTE_ADDR']时,会返回多个IP地址,例如'155.240.132.261,196.250.25.120‘。当此字符串作为gethostbyaddr()的参数传递时,PHP会显示以下错误:
警告:地址不是有效的IPv4或IPv6地址...
为了解决这个问题,我使用以下代码从字符串中提取第一个IP地址,并丢弃其余的地址。(如果您希望使用其他in,它们将位于$ips数组的其他元素中)。
if (strstr($remoteIP, ', ')) {
$ips = explode(', ', $remoteIP);
$remoteIP = $ips[0];
}https://stackoverflow.com/questions/3077862
复制相似问题