Skip to content

Instantly share code, notes, and snippets.

@andrewkdouglas
Created March 12, 2013 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkdouglas/5145899 to your computer and use it in GitHub Desktop.
Save andrewkdouglas/5145899 to your computer and use it in GitHub Desktop.
A simple IP filter for detecting robots by IP address range.
<?php
class IPControl {
public static $ROBOT_IP_RANGES = array(
array(
'name'=>'Monitoring Server',
'min'=>'173.203.5.0',
'max'=>'173.203.5.127',
),
array(
'name'=>'Search Server',
'min'=>'173.203.5.192',
'max'=>'173.203.5.255',
),
);
public static function isKnownRobot($clientIP = null) {
try{
if(!$clientIP){
$clientIP = $_SERVER['REMOTE_ADDR'];
}
foreach(IPControl::$ROBOT_IP_RANGES as $ipRange){
$startRange = ip2long($ipRange['min']);
$endRange = ip2long($ipRange['max']);
$userIP = ip2long($clientIP);
if ( $userIP >= $startRange && $userIP <= $endRange) {
return true;
}
}
return false;
}
catch(Exception $e){
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment