public static function IPTools::validateCIDR in Restrict Login or Role Access by IP Address 8.4
Given an singe IP address and a range of IP addresses in CIDR notation, validate that the single IP address is withing the range of IP addresses.
Overrides IPToolsInterface::validateCIDR
1 call to IPTools::validateCIDR()
- IPTools::validateIP in src/
IPTools.php - Given a string, validate that it is valid IP address.
File
- src/
IPTools.php, line 81
Class
- IPTools
- Class IPTools.
Namespace
Drupal\restrict_by_ipCode
public static function validateCIDR($ip, $range) {
// Separate ip address and CIDR mask.
$netmask = explode("/", $range);
// Get valid network as long.
$ip_net = ip2long($netmask[0]);
// Get valid network mask as long.
$ip_mask = ~((1 << 32 - $netmask[1]) - 1);
// Get ip address to check as long.
$ip_ip = ip2long($ip);
// Mask ip address to check to get subnet.
$ip_ip_net = $ip_ip & $ip_mask;
// Only returns 1 if the valid network
// and the subnet of the ip address
// to check are the same.
if ($ip_ip_net != $ip_net) {
throw new IPOutOfRangeException('IP address and routing prefix are not a valid combination.');
}
}