Let's see how to validate an ip address.
Use the filter_Var() built-in function.
The argument uses the constant FILTER_VALIDATE_IP.
- filter_Var('ip',FILTER_VALIDATE_IP);
The first argument uses the ip address, the second uses the constant FILTER_VALIDATE_IP.
As shown in the example, you can change the purpose according to the value of a constant.
Returns true if the ip address is valid or false.
The following example validates ip.
- <?php
- $ip = '192.168.0.1';
- $checkIp = filter_Var($ip, FILTER_VALIDATE_IP);
- if($checkIp == true) {
- echo "correct ip";
- } else {
- echo "The ip address is not correct.";
- }
- ?>
result
This time let's test by entering the wrong ip address.
- <?php
- $ip = '192.168.0';
- $checkIp = filter_Var($ip, FILTER_VALIDATE_IP);
- if($checkIp == true) {
- echo "correct ip";
- } else {
- echo "The ip address is not correct.";
- }
- ?>
result