Skip to content

Instantly share code, notes, and snippets.

@techi602
Last active December 20, 2015 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save techi602/6156660 to your computer and use it in GitHub Desktop.
Save techi602/6156660 to your computer and use it in GitHub Desktop.
PHP match IPv4 address range
<?php
/**
* Matches
* XXX.XXX.XXX.XXX
* XXX.XXX.XXX.XXX-ZZZ
* XXX.XXX.XXX.XXX-XXX.XXX.XXX.ZZZ
*
*
* @param string $range
* @param string $ip
* @return boolean
*/
public static function matchIP($range, $ip)
{
$range = trim($range);
if ($range == $ip) {
return true;
}
$parts = explode('-', $range);
if (count($parts) == 2) {
$iplong = ip2long($ip);
if (strstr($parts[1], '.') === false) { // muze byt jen XXX.XXX.XXX.XXX-ZZZ
$parts[1] = substr($parts[0], 0, strrpos($parts[0], '.')) . '.' . $parts[1];
}
if (($iplong >= ip2long($parts[0])) && ($iplong <= ip2long($parts[1]))) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment