Home » Php » filter – Validate IPv4 and IPv6 address, excluding private range addresses in php

filter – Validate IPv4 and IPv6 address, excluding private range addresses in php

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to validate IP’s Excluding Reserved and Private Ranges (e.g. 190.167.1.1), Before storing IP’s into database.

So, My function looks like this:

if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  $_SESSION['message'] = "it's valid";
}
else {
  $_SESSION['message'] = "it's not valid";
}

Am I doing right or is there a beter way to do that without using regular expressions ?

How to&Answers: