I want to check if an entered input is a valid IP address or not. I would like a specific function that will help me validate a users input.
A few of the common reasons why an IP address becomes invalid are using reserved IP address ranges, using invalid IP addresses, using non-standard IP address formats, outdated IP addresses, DNS configuration issues, network card problems, and IP address conflicts.
We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.
filter_var($ip, FILTER_VALIDATE_IP)
http://www.php.net/filter_var
Example:-
if(filter_var($ip, FILTER_VALIDATE_IP)){ echo 'Valid IP'; } else { echo 'Not Valid IP'; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With