第一种:
IP通过ip2long转换后可以进行比较。
<?php
/**
* 判断IP是否在某个网络内
* @param $ip
* @param $network
* @return bool
*/
function ip_in_network($ip, $network)
{
$ip = (double) (sprintf("%u", ip2long($ip)));
$s = explode(‘/‘, $network);
$network_start = (double) (sprintf("%u", ip2long($s[0])));
$network_len = pow(2, 32 - $s[1]);
$network_end = $network_start + $network_len - 1;
if ($ip >= $network_start && $ip <= $network_end)
{
return true;
}
return false;
}
第二种:
//案例:判断192.168.1.127是否在 (192.168.1.1--192.168.1.255)的范围里面
$ip_start = get_iplong(‘192.168.1.1‘); //起始ip
$ip_end = get_iplong(‘192.168.1.255‘);//至ip
$ip = get_iplong(‘192.168.1.127‘);//判断的ip
//可以这样简单判断
if($ip>=$ip_start && $ip <=$ip_end){
echo ‘IP在此范围内‘;
}else{
echo ‘IP不在此范围‘;
}
/**
* 将ip地址转换成int型
* @param $ip ip地址
* @return number 返回数值
*/
function get_iplong($ip){
//bindec(decbin(ip2long(‘这里填ip地址‘)));
//ip2long();的意思是将IP地址转换成整型 ,
//之所以要decbin和bindec一下是为了防止IP数值过大int型存储不了出现负数。
return bindec(decbin(ip2long($ip)));
}
转载自:https://blog.csdn.net/jalin107/article/details/51396886https://blog.csdn.net/u010111874/article/details/51744253
原文:https://www.cnblogs.com/aididiao/p/13362624.html