How to Check an Ip Address Is Within a Range of Two Ips in PHP

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

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

PHP Check if IP Address is in a range of IP Addresses

This is something small I came up with. I am sure there are other ways to check, but this will do for my purposes.

For example if I want to know if the IP Address 10.0.0.1 is between the range 10.0.0.1 and 10.1.0.0 then I would run the following command.

var_dump(ip_in_range("10.0.0.1", "10.1.0.0", "10.0.0.1")); 

And in this case it returns true confirming that the IP Address is in the range.

    # We need to be able to check if an ip_address in a particular range
function ip_in_range($lower_range_ip_address, $upper_range_ip_address, $needle_ip_address)
{
# Get the numeric reprisentation of the IP Address with IP2long
$min = ip2long($lower_range_ip_address);
$max = ip2long($upper_range_ip_address);
$needle = ip2long($needle_ip_address);

# Then it's as simple as checking whether the needle falls between the lower and upper ranges
return (($needle >= $min) AND ($needle <= $max));
}

Given an IP address and a Subnet, how do I calculate the range of IPs using php

The below worked perfectly

function isInRange() {

//--- IP range
$IPRange = array (
array("address"=>"197.207.35.238","subnet"=>"255.255.0.0"),
array("address"=>"41.207.44.232","subnet"=>"255.255.10.0"),
array("address"=>"40.207.44.250","subnet"=>"255.255.0.0")
);

foreach ($IPRange as $pair) {

//Check if we have subnet mask
if ($pair['subnet']!='') {
// simple example
$bcast = ip2long($_SERVER['REMOTE_ADDR']);
$smask = ip2long($pair['subnet']);
$nmask = $bcast & $smask;
$SourceStartIP = long2ip($nmask);

if($SourceStartIP==$pair['address']) {
//This is in range
return true;
}

} else {

//--- The header matches something in the fixed list
if ($pair['address'] == $_SERVER['REMOTE_ADDR']) {

return true;

}

}
}

return false;

}

How do you check if an IP range is private or not in PHP?

All private ranges in the IP space are separated by public ranges (p for private, _ for public):

__ppp___pppp___ppp___

If you want a user defined range to be completely private, it needs to be fully contained in one of the private ranges (u for user-defined all-private ranges = hits, x for wrong ranges, fails):

__ppp___pppp___ppp___
uu
u
xxxxxxx
uu
u

Thus, we need to check whether both input values ($start and $end) are not only private, but also in the same private range.

function checkRange($start, $end)
{
$start = ip2long($start);
$end = ip2long($end);

if (!$start || !$end)
throw new Exception('Invalid input.');

if ($start > $end)
throw new Exception('Invalid range.'); // Alternative: switch $start and $end

$range1_start = ip2long('10.0.0.0');
$range1_end = ip2long('10.255.255.255');
$range2_start = ip2long('172.16.0.0');
$range2_end = ip2long('172.31.255.255');
$range3_start = ip2long('192.168.0.0');
$range3_end = ip2long('192.168.255.255');

return ($start >= $range1_start && $start <= $range1_end &&
$end >= $range1_start && $end <= $range1_end) ||
($start >= $range2_start && $start <= $range2_end &&
$end >= $range2_start && $end <= $range2_end) ||
($start >= $range3_start && $start <= $range3_end &&
$end >= $range3_start && $end <= $range3_end);

}

Get all IP addresses in a range in PHP

Wow, there's almost 1000 IP ranges in the list! To optimize verifying a single IP address is in one of the IP ranges, you definitely should do some pre-processing of the IP range list. Creating a database is one way. I'd look at SQLite as it is a super-fast local file format that can be faster for read-only tasks like this than sending a query to another process (or machine) running a database.

Create a table with columns "range_start" and "range_end" and indexes on these columns. For each IP address range, insert one row with the start and end of the range. You'll have less than 1000 rows when done. Now, to test an IP address ...

SELECT * FROM IP_RANGES WHERE $testIP>=range_start AND $testIP<=range_end

(modify this SQL statement to match how you're calling the database, preferably with PDO, and preferably in an SQL-injection safe way).

UPDATE: Sorry, missed the comments above while I was typing my answer.

@Ukuser32, yes, use ip2long to convert dotted decimal to long ints (much easier to work with; store long ints in the database). To get the end of the range, look at the 2nd part of the CIDR. It is the number (N) of bits, starting from the left, that are fixed within the range. That means 32-N is the number of variable bits. Which means there are 2**(32-N) ips in the range. Which means $range_end can be calculated ...

list($range_start, $range_end) = cidr2range('40.112.124.0/24');
echo "Start: $range_start, end: $range_end\n";

function cidr2range($cidr) {
$cidr = explode('/', $cidr);
$range_start = ip2long($cidr[0]);
$range_end = $range_start + pow(2, 32-intval($cidr[1])) - 1;
return [$range_start, $range_end];
}


Related Topics



Leave a reply



Submit