FILTER_VALIDATE_IP filter The filter validates the value as an IP address.
Name: "validate_ip"
ID-number: 275
Possible signs:
FILTER_FLAG_IPV4 - requires the value to be a legal IPv4 IP (such as 255.255.255.255).
FILTER_FLAG_IPV6 - requires the value to be a valid IPv6 IP (e.g. 2001:0db8:85a3:08d3:1319:8a2e:0370:7334).
FILTER_FLAG_NO_PRIV_RANGE - Requires a value not within the private range IP specified by the RFC (eg 192.168.0.1).
FILTER_FLAG_NO_RES_RANGE - Requires value not within reserved IP range. This flag accepts IPV4 and IPV6 values.
<?php$ip = "192.168.0.1";if(!filter_var($ip, FILTER_VALIDATE_IP)) { echo "IP is not valid"; }else { echo "IP is valid"; }?>
The output of the code looks like this:
IP is valid
<?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"; }?>
The output of the code looks like this:
IP is valid