Latest web development tutorials

PHP FILTER_VALIDATE_IP filter

PHP Filter Reference Complete PHP Filter Reference

Definition and Usage

FILTER_VALIDATE_IP filter filters the value as an IP address to verify.

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

Possible signs:

  • FILTER_FLAG_IPV4 - demand value is a legitimate IPv4 IP (eg 255.255.255.255).
  • FILTER_FLAG_IPV6 - required value is legal IPv6 IP (eg 2001: 0db8: 85a3: 08d3: 1319: 8a2e: 0370: 7334).
  • FILTER_FLAG_NO_PRIV_RANGE - Requirements RFC value is not within the specified range of IP private (eg 192.168.0.1).
  • FILTER_FLAG_NO_RES_RANGE - required value within the IP range is not reserved. The flag accepts IPV4 and IPV6 values.

Example 1

<?php
$ip = "192.168.0.1";

if(!filter_var($ip, FILTER_VALIDATE_IP))
{
echo "IP is not valid";
}
else
{
echo "IP is valid";
}
?>

Output code is as follows:

IP is valid


Example 2

<?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";
}
?>

Output code is as follows:

IP is valid


PHP Filter Reference Complete PHP Filter Reference