FILTER_VALIDATE_IP constant in PHP

PHPProgrammingServer Side Programming

The FILTER_VALIDATE_IP constant validates an IP address.

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

Return

The FILTER_VALIDATE_IP constant does not return anything.

Example

Live Demo

<?php
   $ip = "35.2.1";
   if (filter_var($ip, FILTER_VALIDATE_IP)) {
      echo("Valid IP address!");
   } else {
      echo("Invalid IP address!");
   }
?>

The following is the output.

Invalid IP address!

Let us see another example.

Live Demo

<?php
   $ip = "127.0.0.1";
   if (filter_var($ip, FILTER_VALIDATE_IP)) {
      echo("Valid IP address!");
   } else {
      echo("Invalid IP address!");
   }
?>

Here is the output.

Valid IP address!
raja
Published on 04-Dec-2018 07:11:43
Advertisements