public static function v4InRange($ipAddress, $range)
    {
        if (strpos($range, '/') !== false) {
            // $range is in IP/NETMASK format
            list($range, $netMask) = explode('/', $range, 2);

            if (strpos($netMask, '.') !== false) {
                // $netMask is a 255.255.0.0 format
                $netMask = str_replace('*', '0', $netMask);
                $netMaskDec = ip2long($netMask);

                return ((ip2long($ipAddress) & $netMaskDec) === (ip2long($range) & $netMaskDec));
            }

            // $netMask is a CIDR size block
            // fix the range argument
            $blocks = explode('.', $range);

            while (count($blocks) < 4) {
                $blocks[] = '0';
            }

            list($blockA, $blockB, $blockC, $blockD) = $blocks;

            $range = sprintf(
                '%u.%u.%u.%u',
                empty($blockA) ? '0' : $blockA,
                empty($blockB) ? '0' : $blockB,
                empty($blockC) ? '0' : $blockC,
                empty($blockD) ? '0' : $blockD
            );

            $rangeDec = ip2long($range);
            $ipDec = ip2long($ipAddress);

            $wildcardDec = (2 ** (32 - $netMask)) - 1;
            $netMaskDec = ~$wildcardDec;

            return (($ipDec & $netMaskDec) === ($rangeDec & $netMaskDec));
        }

        // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
        if (strpos($range, '*') !== false) { // a.b.*.* format
            // Just convert to A-B format by setting * to 0 for A and 255 for B
            $lower = str_replace('*', '0', $range);
            $upper = str_replace('*', '255', $range);
            $range = "$lower-$upper";
        }

        if (strpos($range, '-') !== false) { // A-B format
            list($lower, $upper) = explode('-', $range, 2);
            $lowerDec = (float)sprintf('%u', ip2long($lower));
            $upperDec = (float)sprintf('%u', ip2long($upper));
            $ipDec = (float)sprintf('%u', ip2long($ipAddress));
            return (($ipDec >= $lowerDec) && ($ipDec <= $upperDec));
        }

        return false;
    }
public static function v4InRange($ipAddress, $range)
    {
        if (strpos($range, '/') !== false) {
            // $range is in IP/NETMASK format
            list($range, $netMask) = explode('/', $range, 2);

            if (strpos($netMask, '.') !== false) {
                // $netMask is a 255.255.0.0 format
                $netMask = str_replace('*', '0', $netMask);
                $netMaskDec = ip2long($netMask);

                return ((ip2long($ipAddress) & $netMaskDec) === (ip2long($range) & $netMaskDec));
            }

            // $netMask is a CIDR size block
            // fix the range argument
            $blocks = explode('.', $range);

            while (count($blocks) < 4) {
                $blocks[] = '0';
            }

            list($blockA, $blockB, $blockC, $blockD) = $blocks;

            $range = sprintf(
                '%u.%u.%u.%u',
                empty($blockA) ? '0' : $blockA,
                empty($blockB) ? '0' : $blockB,
                empty($blockC) ? '0' : $blockC,
                empty($blockD) ? '0' : $blockD
            );

            $rangeDec = ip2long($range);
            $ipDec = ip2long($ipAddress);

            $wildcardDec = (2 ** (32 - $netMask)) - 1;
            $netMaskDec = ~$wildcardDec;

            return (($ipDec & $netMaskDec) === ($rangeDec & $netMaskDec));
        }

        // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
        if (strpos($range, '*') !== false) { // a.b.*.* format
            // Just convert to A-B format by setting * to 0 for A and 255 for B
            $lower = str_replace('*', '0', $range);
            $upper = str_replace('*', '255', $range);
            $range = "$lower-$upper";
        }

        if (strpos($range, '-') !== false) { // A-B format
            list($lower, $upper) = explode('-', $range, 2);
            $lowerDec = (float)sprintf('%u', ip2long($lower));
            $upperDec = (float)sprintf('%u', ip2long($upper));
            $ipDec = (float)sprintf('%u', ip2long($ipAddress));
            return (($ipDec >= $lowerDec) && ($ipDec <= $upperDec));
        }

        return false;
    }
public static function v4InRange ( $ ipAddress , $ range ) { if ( strpos ( $ range , '/' ) !== false ) { // $range is in IP/NETMASK format list ( $ range , $ netMask ) = explode ( '/' , $ range , 2 ) ; if ( strpos ( $ netMask , '.' ) !== false ) { // $netMask is a 255.255.0.0 format $ netMask = str_replace ( '*' , '0' , $ netMask ) ; $ netMaskDec = ip2long ( $ netMask ) ; return ( ( ip2long ( $ ipAddress ) & $ netMaskDec ) === ( ip2long ( $ range ) & $ netMaskDec ) ) ; } // $netMask is a CIDR size block // fix the range argument $ blocks = explode ( '.' , $ range ) ; while ( count ( $ blocks ) < 4 ) { $ blocks [ ] = '0' ; } list ( $ blockA , $ blockB , $ blockC , $ blockD ) = $ blocks ; $ range = sprintf ( '%u.%u.%u.%u' , empty ( $ blockA ) ? '0' : $ blockA , empty ( $ blockB ) ? '0' : $ blockB , empty ( $ blockC ) ? '0' : $ blockC , empty ( $ blockD ) ? '0' : $ blockD ) ; $ rangeDec = ip2long ( $ range ) ; $ ipDec = ip2long ( $ ipAddress ) ; $ wildcardDec = ( 2 ** ( 32 - $ netMask ) ) - 1 ; $ netMaskDec = ~ $ wildcardDec ; return ( ( $ ipDec & $ netMaskDec ) === ( $ rangeDec & $ netMaskDec ) ) ; } // range might be 255.255.*.* or 1.2.3.0-1.2.3.255 if ( strpos ( $ range , '*' ) !== false ) { // a.b.*.* format // Just convert to A-B format by setting * to 0 for A and 255 for B $ lower = str_replace ( '*' , '0' , $ range ) ; $ upper = str_replace ( '*' , '255' , $ range ) ; $ range = "$lower-$upper" ; } if ( strpos ( $ range , '-' ) !== false ) { // A-B format list ( $ lower , $ upper ) = explode ( '-' , $ range , 2 ) ; $ lowerDec = ( float ) sprintf ( '%u' , ip2long ( $ lower ) ) ; $ upperDec = ( float ) sprintf ( '%u' , ip2long ( $ upper ) ) ; $ ipDec = ( float ) sprintf ( '%u' , ip2long ( $ ipAddress ) ) ; return ( ( $ ipDec >= $ lowerDec ) && ( $ ipDec <= $ upperDec ) ) ; } return false ; }
Check if a given ip is in a network

@param  string $ipAddress IP to check in IPV4 format eg. 127.0.0.1
@param  string $range     IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed

@return bool
@SuppressWarnings(PHPMD.NPathComplexity)
@see https://www.pgregg.com/projects/php/ip_in_range/
Check if a given ip is in a network