Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Created September 8, 2011 05:19
Show Gist options
  • Save AshKyd/1202686 to your computer and use it in GitHub Desktop.
Save AshKyd/1202686 to your computer and use it in GitHub Desktop.
Determine if an IP address is coming from a local network.
/**
* Is this a local IP?
* @param {string} $ip The IP address you wish to checkk.
* @return {boolean} Whether the IP is visiting from a local network.
*/
function isLocalIp($ip){
// Some local IP ranges.
$ranges = Array(
Array('10.0.0.0','10.255.255.255'),
Array('172.16.0.0','172.31.255.255'),
Array('192.168.0.0','192.168.255.255')
);
// Convert IP to long.
$ip = ip2long($ip);
foreach($ranges as $i){
// Check if IP is between the predefined IP ranges.
if($ip >= ip2long($i[0]) && $ip <= ip2long($i[1])){
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment