Is this a private IP address?

I need to show different content to folks who are outside our network.
This snippet will detect a private ip address using php's built in filter class.

function is_private_ip($ip) {
return !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
}

FILTER_FLAG_NO_PRIV_RANGE gets you:

"Fails validation for the following private IPv4 ranges:10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.

Fails validation for the IPv6 addresses starting with FDor FC."

and FILTER_FLAG_NO_RES_RANGE gets you:

"Fails validation for the following reserved IPv4 ranges:0.0.0.0/8, 169.254.0.0/16, 192.0.2.0/24 and224.0.0.0/4. This flag does not apply to IPv6 addresses."

Source: PHP Docs

 

derak

 

Leave a Reply

Your email address will not be published. Required fields are marked *