我希望检测地址IP是本地还是外部。 在Web应用程序中,我的客户希望通过本地网络上的samba直接访问文件,如果您不在网络中,则需要下载链接。
我可能只是检查我的IP是否是172.30.*.* ,但如果我的客户切换到1 0.*.*.*或IPV6,它将不再起作用。
我可以使用一个配置变量,所以客户可以随意更改它。 我希望它是自动的,没有配置变种。
你会怎么做?
I wish to detect if an address IP is local or external. In a web application, my customer wish to access files directly through samba on local network, and by a download link if you are not in the network.
I may just check if my IP is 172.30.*.*, but it won't work anymore if my customer switch to a 10.*.*.* or IPV6.
I may use a config var, so the customer may change it at will. I would like it to be automatic, with no config var.
How would you proceed ?
最满意答案
请随意更新IP范围列表。 我有一段时间没有更新这个列表,但它应该有最多。 对不起,我从未添加过IPv6支持。
function reserved_ip($ip) { $reserved_ips = array( // not an exhaustive list '167772160' => 184549375, /* 10.0.0.0 - 10.255.255.255 */ '3232235520' => 3232301055, /* 192.168.0.0 - 192.168.255.255 */ '2130706432' => 2147483647, /* 127.0.0.0 - 127.255.255.255 */ '2851995648' => 2852061183, /* 169.254.0.0 - 169.254.255.255 */ '2886729728' => 2887778303, /* 172.16.0.0 - 172.31.255.255 */ '3758096384' => 4026531839, /* 224.0.0.0 - 239.255.255.255 */ ); $ip_long = sprintf('%u', ip2long($ip)); foreach ($reserved_ips as $ip_start => $ip_end) { if (($ip_long >= $ip_start) && ($ip_long <= $ip_end)) { return TRUE; } } return FALSE; } var_dump(reserved_ip('127.0.0.1')); // reserved (localhost) var_dump(reserved_ip('74.125.140.101')); // not reserved (Google)Update the list of IP ranges as you please. I haven't updated this list in awhile, but it should have most. Sorry, I never added IPv6 support to it.
function reserved_ip($ip) { $reserved_ips = array( // not an exhaustive list '167772160' => 184549375, /* 10.0.0.0 - 10.255.255.255 */ '3232235520' => 3232301055, /* 192.168.0.0 - 192.168.255.255 */ '2130706432' => 2147483647, /* 127.0.0.0 - 127.255.255.255 */ '2851995648' => 2852061183, /* 169.254.0.0 - 169.254.255.255 */ '2886729728' => 2887778303, /* 172.16.0.0 - 172.31.255.255 */ '3758096384' => 4026531839, /* 224.0.0.0 - 239.255.255.255 */ ); $ip_long = sprintf('%u', ip2long($ip)); foreach ($reserved_ips as $ip_start => $ip_end) { if (($ip_long >= $ip_start) && ($ip_long <= $ip_end)) { return TRUE; } } return FALSE; } var_dump(reserved_ip('127.0.0.1')); // reserved (localhost) var_dump(reserved_ip('74.125.140.101')); // not reserved (Google)更多推荐
发布评论