Skip to content

Instantly share code, notes, and snippets.

@DaHoC
Last active December 18, 2018 12:53
Show Gist options
  • Save DaHoC/3680e4c0d8f2cd7d4528324172a96a17 to your computer and use it in GitHub Desktop.
Save DaHoC/3680e4c0d8f2cd7d4528324172a96a17 to your computer and use it in GitHub Desktop.
IP range check in PHP - This function checks if a given IP address is in a given IP range. It should be much more efficient than the ip2long / long2ip functions that come with PHP that can only be used for IPv4 and have polynomial costs
<?php
/*
> CONTACT:
Jan Hendriks
http://www.gawe-design.de
info@gawe-design.de
> DESCRIPTION:
The functions (you can also use them via php-include) checks if a given IP is in a given IP range
These functions are much more efficient than the ip2long / long2ip functions from php that can only be used for IP v4 and have polynomial costs
These functions are about O(number of positions), e.g. IP v.4: O(4); (I think)
And without all these stupid comments it is really tiny!
> EXAMPLE OF USE:
You could use them for example as follows:
// remove the $ip, $ipra, $ipre and echo lines on top of this file
include("testip.php"); // In your script
$ip="127.0.01"; // Define the IP to check
// e.g. $ip = gethostbyname("www.dahoc.de.vu"); whereas 'gethostbyname' feature has to be enabled by server
$ipra="127.0.0.0" // Your IP range start, has to be "smaller" or equal than the IP range end
$ipre='127.0.0.255'; // Your IP range end
echo '<br/>IP is '.(isInIpRange($ip) ? '' : 'not ').'in the given range'; // The actual invocation of the check, taken from below
> COPYRIGHT:
no reproduction without my permission (please just ask me first)
> Comments, corrections, requests, suggestions, bugfixes, complaints and questions are always welcome
*/
$ip='127.9.9.9.9.9'; // example IP to check
// IP-Range start and end
// IMPORTANT: start-IP has to be smaller or equal than end-IP!
// IMPORTANT AND OBVIOUS: IP range has to have the same length of the IP to check!
$ipra='127.0.0.1.5.2'; // example IP range start
$ipre='127.9.9.9.9.9'; // example IP range end
echo '<br/>IP ('.$ip .') is '.(isInIpRange($ip) ? '' : 'not ').'in the given range ('.$ipra.' - '.$ipre.')'; // Explicitly: if (isInIpRange($ip)) { /* IP is in range */ } else { /* IP is out of range */ }
// boolean function checks if given IP address matches IP Range
/** @return true: IP address is in IP range
@return false: IP address is out of IP range **/
function isInIpRange($theip) {
global $ipra;
global $ipre;
// Split those numbers up, no matter if this is IP v4, IP v6, ...
$aip=(array)explode('.',$theip);
// IP is 'not in Range' easier to check than 'IP is in range':
// That's because if the first if (testip()...) fails, the second is needless, so that we thus save some cost
// Check if IP is not smaller than starting IP and is not bigger than ending IP = IP is in range
return (bool)( (testIp($aip,(array)explode('.',$ipra)) != -1) && (testIp($aip,(array)explode('.',$ipre)) != 1) );
}
// recursive function compares 2 (IP) numbers
/** @return -1: First number smaller than second
@return 1: First number bigger than second
@return 0: First and second number equal **/
// $i specifies the position of the check, initialized with 0 (top position of IP-Tuple)
function testIp($theip,$theipr,$i=0) {
switch (TRUE) {
case ($theip[$i] < $theipr[$i]): return (int)-1;
case ($theip[$i] > $theipr[$i]): return (int)1;
case (count($theip) == ($i+1)): return (int)0; // Last position of the IP-tuple reached and additional all numbers up to this place are equal => Return equal: 0
default: return (int)testIp($theip,$theipr,++$i); // If equal at current position, check next position (recursive) e.g. 127.x.y.z == 127.k.l.m (next would be to test position x against k, y against l, etc.)
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment