Re: How to ask "if private IP"?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ronald Wiplinger wrote:
> I use $aa=$_SERVER["REMOTE_ADDR"];
> 
> and
> 
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
>     $aa="61.64.101.101";    // for testing put in a public IP   
> }
> 
> 
> However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> and 172.??.x.x). How can I do that simple?

probably not the best or fastest way to do it but here is what I use:

function isPrivateIP($theip)
{
    foreach (array("10.0.0.0/8",
                   "172.16.0.0/12",
                   "192.168.0.0/16") as $subnet)
    {
        list($net, $mask) = explode('/', $subnet);
        if(isIPInSubnet($theip, $net, $mask))
            return true;
    }

    return false;
}

function isIPInSubnet($ip, $net, $mask)
{
    $firstpart  = substr(str_pad(decbin(ip2long($net)), 32, "0", STR_PAD_LEFT) ,0 , $mask);
    $firstip    = substr(str_pad(decbin(ip2long($ip)), 32, "0", STR_PAD_LEFT), 0, $mask);

    return (strcmp($firstpart, $firstip) == 0);
}


> 
> bye
> 
> Ronald
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux