How to use FILTER VALIDATE IP in PHP

This article describe about FILTER_VALIDATE_IP Filter in PHP.
  • 2085

FILTER_VALIDATE_IP Filter

For validates the value as an IP address use FILTER_VALIDATE_IP Filter.

  • Name: "validate_ip"
  • ID-number: 275

Possible flags:

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

Example 1

<html>
<body>
<?php
$ip = "192.168.0.1";
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
echo "IP is not valid";
}
else
{
echo "IP is valid";
}
?>
</body>
</html>

 

Output


filtervalidateipphp1.jpg

 

Example 2

 

<html>
<body>
<?php
$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
{
echo "IP is not valid";
}
else
{
echo "IP is valid";
}
?>
</body>
</html>

 

Output


filtervalidateipphp2.jpg

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.