Checking for private ip ranges...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • StinkFinger

    Checking for private ip ranges...

    Hello all,
    I am trying to update a function of mine to detect for private ip ranges.
    Here is what I have so far, and of course, I am stuck at the part where i
    need to compare the $ip to the $private_ip array to see if there is a match.

    can anyone help out ? i am searching now on preg_replace, i think that is
    where i need to start, but not 100% sure.
    Thanks.

    function getipaddress()
    {
    $ip;
    if (getenv("HTTP_C LIENT_IP")) $ip = getenv("HTTP_CL IENT_IP");
    else if (getenv("REMOTE _ADDR")) $ip = getenv("REMOTE_ ADDR");
    else if (getenv("HTTP_X _FORWARDED_FOR" )) $ip =
    getenv("HTTP_X_ FORWARDED_FOR") ;
    else $ip = "UNKNOWN";
    $private_ip = array("/^0\./", "/^127\.0\.0\.1/", "/^192\.168\..*/",
    "/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/", "/^10\..*/", "/^224\..*/",
    "/^240\..*/");
    //do something here if a match, like die()
    return $ip;
    }


  • StinkFinger

    #2
    Re: Checking for private ip ranges...

    after some more searching and tinkering, i came up with this:

    $private_ip = array("/^0\./", "/^127\.0\.0\.1/", "/^192\.168\..*/",
    "/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/", "/^10\..*/", "/^224\..*/",
    "/^240\..*/");
    while (list ($key, $val) = each ($private_ip)) {
    if (preg_match($va l, $ip))
    {
    die("private");
    }
    }

    of course, i won't leave the die() statement there, i'll expand on it later,
    now that i know its working.
    anyone have any comments or other ideas for this ?


    Comment

    Working...