Warning: preg_replace(): Compilation failed: escape sequence is invalid in character class at offset 4 in /home/customer/www/theunixtips.com/public_html/wp-content/plugins/resume-builder/includes/class.resume-builder-enqueues.php on line 59

php : find if IP address is in Network range

Using pear Net_IPv4 module one can find if a given IP address is in provided Network range or on the subnet.

<?php
        // check if IP falls in provided subnet
        include("Net/IPv4.php");

        $ipAddr  = "192.168.1.8";
        $netAddr = "192.168.1.0/29"; // 192.168.1.0 - 192.168.1.7

        $objIP = new Net_IPv4();

        echo $objIP->ipInNetwork($ipAddr, $netAddr) ? "$ipAddr is in $netAddrn" : "$ipAddr is not in $netAddrn";
?>

This requires pear Net_IPv4 module which can be installing in one of the following ways.

pear install Net_IPv4
php pyrus.phar install pear/Net_IPv4