PHP FILTER_VALIDATE_IP Filter

What is FILTER_VALIDATE_IP in PHP?

Explanation

PHP FILTER_VALIDATE_IP is used to check whether the given IP address is valid or not.
Name: "validate_ip"
ID-number: 275

Options:


FILTER_FLAG_IPV4 Requires the value to be a valid IPv4 IP (like 125.155.155.155)
FILTER_FLAG_IPV6 Requires the value to be a valid IPv6 IP (like 2001:0db8:85a3:08d3:1319:8a2e:0370:7334)
FILTER_FLAG_NO_PRIV_RANGE Requires that the value is not within an RFC specified private range IP (like 192.168.0.1)
FILTER_FLAG_NO_RES_RANGE Requires that the value is not within the reserved IP range. This flag takes both IPV4 and IPV6 values

Example :


<?php
$val="125.155.155.155";
if(filter_var($val, FILTER_VALIDATE_IP , FILTER_FLAG_IPV4))
{
echo "Valid Ip";
}
else
{
echo "Invalid Ip";
}
?>

Result :

Valid Ip

PHP Topics


Ask Questions

Ask Question