Created
March 25, 2016 10:11
-
-
Save ivankristianto/c70ab35f0afcbfc6140a to your computer and use it in GitHub Desktop.
Check if my ip is private
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ip_is_private ($ip) { | |
$pri_addrs = array ( | |
'10.0.0.0|10.255.255.255', // single class A network | |
'172.16.0.0|172.31.255.255', // 16 contiguous class B network | |
'192.168.0.0|192.168.255.255', // 256 contiguous class C network | |
'169.254.0.0|169.254.255.255', // Link-local address also refered to as Automatic Private IP Addressing | |
'127.0.0.0|127.255.255.255' // localhost | |
); | |
$long_ip = ip2long ($ip); | |
if ($long_ip != -1) { | |
foreach ($pri_addrs AS $pri_addr) { | |
list ($start, $end) = explode('|', $pri_addr); | |
// IF IS PRIVATE | |
if ($long_ip >= ip2long ($start) && $long_ip <= ip2long ($end)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment