PHP function to check if an IPv4 address is from a private range.

<?php

/**
 * Check if IPv4 address is from a private range.
 *
 * @param string $ip
 * @return boolean true if IPv4 address is in private range.
 */
function ip_is_private ($ip)
{
    $pri_addrs = array(
        '10.0.0.0|10.255.255.255',
        '172.16.0.0|172.31.255.255',
        '192.168.0.0|192.168.255.255',
        '169.254.0.0|169.254.255.255',
        '127.0.0.0|127.255.255.255'
    );

    $long_ip = ip2long($ip);

    if ($long_ip != - 1) {

        foreach ($pri_addrs as $pri_addr) {

            list ($start, $end) = explode('|', $pri_addr);

            // IF IS PRIVATE
            if ($long_ip >= ip2long($start) && $long_ip <= ip2long($end))
                return (TRUE);

        }
    }

    return (FALSE);
}

?>