Last active
December 20, 2015 15:49
-
-
Save techi602/6156660 to your computer and use it in GitHub Desktop.
PHP match IPv4 address range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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