Answer the question
In order to leave comments, you need to log in
Need help redirecting to geo ip, how to leave bots?
It is necessary to make changes to this solution so that the redirect works only for users, the bots go further to the site.
//Получаем IP пользователя
$ip = $_SERVER['REMOTE_ADDR'];
//Функция whois для определения страны по IP\
function whois($ip) {
if ($ip!="") {
$sock = fsockopen ("whois.ripe.net",43,$errno,$errstr);
if ($sock) {
fputs ($sock, $ip."\r\n");
while (!feof($sock)) {
$str.=trim(fgets ($sock,128)." <br>");
}
}
else {
$str.="$errno($errstr)";
return;
}
fclose ($sock);
}
return $str;
}
//Получаем whois данные для IP пользователя
$whois = whois($_SERVER['REMOTE_ADDR']);
//Получаем страну, исходя из whois IP
$need = "country:";
$pos = strpos($whois,$need);
$search = substr($whois,$pos,18);
$excount = explode(":", $search);
$country = trim($excount[1]);
//Ну и сам код редиректа
if($country == "RU") {
header('Location: /ru/);
exit();
}
Answer the question
In order to leave comments, you need to log in
Use user agent bot detection
https://github.com/JayBizzle/Crawler-Detect
Firstly, in theory, it is extremely difficult to distinguish a properly configured bot from a visitor, which is specially done to monitor the site "live".
Secondly, a large number of bots enter with a special user agent containing the word bot. But in parallel with this, a bot with an absolutely normal "human" user agent comes in. There are ready-made libraries, for example, those indicated in the nokimaro answer , but this is not a guarantee, literally tomorrow this mechanism may stop working.
In short, if you just catch explicit bots - look at the user agent and cut obvious bots, you can use something ready-made, but you won’t catch 100% of bots.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question