PhpSnip.com

User Stats

Check IP address matches or is in range

If you need to check if the current user's IP address is either within a set range or matches a given IP use this function. Possible uses include additional security for an authentication script. IP range needs to be in form 12.34.56.78-12.34.67.89

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 354

Source Code ( 16 lines )

function checkIPorRange ($ip_address) {
    if (ereg("-",$ip_address)) {  
        // Range
        $ar = explode("-",$ip_address);
        $your_long_ip = ip2long($_SERVER["REMOTE_ADDR"]);
        if ( ($your_long_ip >= ip2long($ar[0])) && ($your_long_ip <= ip2long($ar[1])) ) {
            return TRUE;
        }
    } else {
        // Single IP
        if ($_SERVER["REMOTE_ADDR"] == $ip_address) {
            return TRUE;
        }
    }
    return FALSE;
}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise