Skip to content

Instantly share code, notes, and snippets.

@titomus
Created February 1, 2017 19:09
Show Gist options
  • Save titomus/9f0f004270c467bd28166c897baa400c to your computer and use it in GitHub Desktop.
Save titomus/9f0f004270c467bd28166c897baa400c to your computer and use it in GitHub Desktop.
Detecter les bots par IP
<?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;
}
#demo
$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
if($test != FALSE) {
//action affichage bot ou cloaking <img draggable="false" class="emoji" alt="😉" src="https://s.w.org/images/core/emoji/2.2.1/svg/1f609.svg">
$bot = $test;
echo "bot présent<br/>";
echo $bot;
} else {
echo "pas de bot";
}
@titomus
Copy link
Author

titomus commented Feb 1, 2017

A retrouver sur l'article: Comment detecter les bots sur un site

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment