gpt4 book ai didi

php - 验证 Googlebot

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:07:10 28 4
gpt4 key购买 nike

我将阻止除大型搜索引擎之外的所有机器人。我的阻止方法之一是检查“语言”:接受语言:如果没有接受语言,机器人的 IP 地址将被阻止到 2037 年。Googlebot 没有接受语言,我想用 DNS 验证它查找

<?php
gethostbyaddr($_SERVER['REMOTE_ADDR']);
?>

可以使用gethostbyaddr吗,有人可以通过我的“gethostbyaddr保护”吗?

最佳答案

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 函数的结果是缓存。

该算法基于 Google 的推荐 - https://support.google.com/webmasters/answer/80553

关于php - 验证 Googlebot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3077862/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com