How to use FILTER_VALIDATE_IP for an array?

Asked

Viewed 30 times

0

 <input type="text" name="ip[]"/>

<?php
$ip = $_POST['ip'];

if(filter_var($ip, FILTER_VALIDATE_IP)) {
    echo("$ip is a valid IP address");
   }else {
echo("$ip is not a valid IP address");
}
?>

If I put any ip without being array inside the variable that receives my ip and validates it quietly, but when I pass an array, since the input field is dynamic, php does not accept validation.

I even tried to use FILTER_REQUIRE_ARRAY, but it didn’t work.

1 answer

0


Well, in this case you will have as response an array, your $ip variable will be like this:

array(
    0=>ip_1,
    1=>ip_2
    ...
)

To validate this, you will need to use a loop to traverse the keys and validate key by key.

It would be something like this:

foreach($ip = $i){
    if(filter_var($i, FILTER_VALIDATE_IP)) {
        echo("$i is a valid IP address");
    }else {
        echo("$i is not a valid IP address");
    }
}

See if it helps you there.

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.