Skip to content

Instantly share code, notes, and snippets.

@geilt
Last active December 16, 2015 15:29
Show Gist options
  • Save geilt/5455959 to your computer and use it in GitHub Desktop.
Save geilt/5455959 to your computer and use it in GitHub Desktop.
Checks for proxies as well as the most common bots for pinterest google and twitter when using the share features.
<?php
function isBot(){
if(!empty($_SERVER['VIA'])):
return true;
endif;
if(!empty($_SERVER['REMOTE_PORT']) && in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554))):
return true;
endif;
//if(!empty($_SERVER['REMOTE_ADDR']) && fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30)): // this Causes Lag...
// return false;
//endif;
if(empty($_SERVER['HTTP_ACCEPT_ENCODING']) || empty($_SERVER['HTTP_CONNECTION'])):
return true;
endif;
if(!empty($_SERVER['HTTP_VIA'])):
return true;
endif;
if(!empty($_SERVER['FORWARDED'])):
return true;
endif;
if(!empty($_SERVER['USERAGENT_VIA'])):
return true;
endif;
if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])):
return true;
endif;
if(!empty($_SERVER['HTTP_X_FORWARDED_HOST'])):
return true;
endif;
if(!empty($_SERVER['HTTP_X_FORWARDED_SERVER'])):
return true;
endif;
if(!empty($_SERVER['PROXY_CONNECTION'])):
return true;
endif;
if(!empty($_SERVER['XPROXY_CONNECTION'])):
return true;
endif;
if(!empty($_SERVER['HTTP_PC_REMOTE_ADDR'])):
return true;
endif;
if(!empty($_SERVER['HTTP_CLIENT_IP'])):
return true;
endif;
//Filter Facebook Bot.
if(strpos($_SERVER['HTTP_USER_AGENT'],'+http://www.facebook.com/externalhit_uatext.php')):
return true;
endif;
//Filter Google+ Snippet.
if(strpos($_SERVER['HTTP_USER_AGENT'],'+https://developers.google.com/+/web/snippet/')):
return true;
endif;
//Filter Google Bot - Initiates after successful share.
if(strpos($_SERVER['HTTP_USER_AGENT'],'+http://www.google.com/bot.html')):
return true;
endif;
//Filter Pinterest Bot.
if(strpos($_SERVER['HTTP_USER_AGENT'],'+http://pinterest.com/')):
return true;
endif;
//Allow only Mozilla and Opera Based Browsers.
if(!preg_match( '/^(Mozilla|Opera)/', $_SERVER['HTTP_USER_AGENT'])):
return true;
endif;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment