Saturday 13 April 2013

php check if IP lies in IP Range



first write function to check ip range

function Iprange_testing($iprange, $iptocheck)
{

    $ip = $iptocheck;


        list ($ip1, $ip2) = explode('-', $iprange);
        $ips = Array($ip,$ip1,$ip2);
        // sort ips and check if client ip is middle one
        natsort($ips);

        $sorted=implode("-", $ips);
        $ips=explode("-", $sorted);

        $ipcheck = true;

        if ($ips[1]!=$ip)
        {

            $ipcheck=false;
        }
return $ipcheck ;
    }

Now call this method

$res1  = Iprange_testing('192.168.1.10-192.168.1.100', '192.168.1.50');  //returns ture
$res2  = Iprange_testing('192.168.1.10-192.168.1.100', '192.168.1.101');  //returns false


1 comment: