How to check an IP address is within a range of two IPs in PHP?

Issue

I have an IP address and I’m given two other IP addresses which together creates an IP range. I want to check if the first IP address is within this range. How can i find that out in PHP?

Solution

With ip2long() it’s easy to convert your addresses to numbers. After this, you just have to check if the number is in range:

if ($ip <= $high_ip && $low_ip <= $ip) {
  echo "in range";
}

Answered By – oezi

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.