QUIZGUM

Coding Class

Quizgum : ip validation

Validate ip address

Let's see how to validate an ip address.
Use the filter_Var() built-in function.
The argument uses the constant FILTER_VALIDATE_IP.

How to validate ip address using filter_var() function

  1. 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.

  1. <?php
  2. $ip = '192.168.0.1';
  3.  
  4. $checkIp = filter_Var($ip, FILTER_VALIDATE_IP);
  5.  
  6. if($checkIp == true) {
  7. echo "correct ip";
  8. } else {
  9. echo "The ip address is not correct.";
  10. }
  11. ?>

result

This time let's test by entering the wrong ip address.

  1. <?php
  2. $ip = '192.168.0';
  3.  
  4. $checkIp = filter_Var($ip, FILTER_VALIDATE_IP);
  5.  
  6. if($checkIp == true) {
  7. echo "correct ip";
  8. } else {
  9. echo "The ip address is not correct.";
  10. }
  11. ?>

result