Skip to content

Instantly share code, notes, and snippets.

@titomus
Created August 10, 2017 07:11
Show Gist options
  • Save titomus/3ec329b3578feb71c334754977ed0601 to your computer and use it in GitHub Desktop.
Save titomus/3ec329b3578feb71c334754977ed0601 to your computer and use it in GitHub Desktop.
Detecter les crawlers des moteurs de recherche
<?php
/**
* Is_bot
* traque la présence d'un bot par plage d'ip
*
* @param string $ip
* @param array $array_bots
* @return string|false
*/
function is_bot ($ip,$array_bots) {
$ip = strval($ip);
$reponse = false;
foreach($array_bots as $bot=>$val) {
foreach($array_bots[$bot] as $ip_bot) {
$reponse = strpos($ip,$ip_bot);
if ($reponse !== FALSE) return $bot;
}
}
return $reponse;
}
// Utilisation
$bots = array(
'google' => array('216.239.46','64.68.8','66.249.','64.233.173'),
'yahoo' => array('66.196.','68.142.','202.165.','66.142.')
);
$test = is_bot('216.239.46.654',$bots); //renverra TRUE car 216.239.46 est présent dans l'ip. Utilisez $_SERVER['REMOTE_ADDR'] pour automatiser
if($test != FALSE) {
//action affichage bot ou cloaking ;)
$bot = $test;
echo "bot présent<br/>";
echo $bot;
} else {
echo "pas de bot";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment