PHP FILTER_VALIDATE_IP Filter
Example
Check if the variable $ip is a valid IP address:
<?php
$ip = "127.0.0.1";
if (filter_var($ip, FILTER_VALIDATE_IP)) {
echo("$ip is a valid IP address");
} else {
echo("$ip is not a valid IP address");
}
?>
Try it Yourself »
Definition and Usage
The FILTER_VALIDATE_IP filter validates an IP address.
Possible flags:
- FILTER_FLAG_IPV4 - The value must be a valid IPv4 address
- FILTER_FLAG_IPV6 - The value must be a valid IPv6 address
- FILTER_FLAG_NO_PRIV_RANGE - The value must not be within a private range
- FILTER_FLAG_NO_RES_RANGE - The value must not be within a reserved range
More Examples
Example 1
Check if the variable $ip is a valid IPv6 address:
<?php
$ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
echo("$ip is a valid IPv6 address");
} else {
echo("$ip is not a valid IPv6 address");
}
?>
Try it Yourself »
❮ PHP Filter Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.