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

guitarlass picture guitarlass · Jun 20, 2012 · Viewed 56.2k times · Source

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?

Answer

oezi picture oezi · Jun 20, 2012

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";
}