Skip to content

Instantly share code, notes, and snippets.

@tott
Created November 27, 2013 22:46
Show Gist options
  • Save tott/7684443 to your computer and use it in GitHub Desktop.
Save tott/7684443 to your computer and use it in GitHub Desktop.
php check if IP is in given network range
/**
* Check if a given ip is in a network
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}
@blazeag
Copy link

blazeag commented Nov 22, 2016

Awesome, thank you very much!

@Komalbandi
Copy link

Thank you very much.

@trenshaw
Copy link

/**
 * Checks if a given IP address matches the specified CIDR subnet/s
 * 
 * @param string $ip The IP address to check
 * @param mixed $cidrs The IP subnet (string) or subnets (array) in CIDR notation
 * @param string $match optional If provided, will contain the first matched IP subnet
 * @return boolean TRUE if the IP matches a given subnet or FALSE if it does not
 */
public function ipMatch($ip, $cidrs, &$match = null) {
	foreach((array) $cidrs as $cidr) {
		list($subnet, $mask) = explode('/', $cidr);
		if(((ip2long($ip) & ($mask = ~ ((1 << (32 - $mask)) - 1))) == (ip2long($subnet) & $mask))) {
			$match = $cidr;
			return true;
		}
	}
	return false;
}

Building on the function by @tott to support multiple CIDRs and optionally set the first matched subnet by reference

@turkhero
Copy link

thanks @trenshaw

@jorgeolivares
Copy link

Thanks!

@selva987
Copy link

Really useful, Thanks!

@phantommax
Copy link

Great! Saved me some priceless time!

@NiMeDia
Copy link

NiMeDia commented Dec 7, 2017

Thanks a lot.
Little optimization: You should do the strpos check with "===" else php matches the first position too, because 0 == false.

@agustin-tiendanube
Copy link

Thank you!

@jwilliam-meneses
Copy link

Very good. Thank you.

@Quix0r
Copy link

Quix0r commented Jul 12, 2018

The === is still not fixed. Else thank you!

@welllima88
Copy link

THAANKSS MAN. very good!

@roman-syrota
Copy link

Thanks!

@hirschtec-ep
Copy link

thx - nice peace!

@EvoWebFrance
Copy link

Very nice... thank you for this code

@patrickscottbest
Copy link

A word of thanks.

@modder2
Copy link

modder2 commented Sep 1, 2020

See also the implementation by cloudflare, supporting ipv4 and v6: https://github.com/cloudflare/CloudFlare-Tools/blob/master/cloudflare/ip_in_range.php

Thanks for the link! This is what I need.

@Pseudodevincent
Copy link

Thanks a lot !

@c7borg
Copy link

c7borg commented Jan 7, 2021

Thank you.. useful for a lot of projects

@miqueiaspenha
Copy link

Thanks!

@irfanhussain22
Copy link

good stuff

@shahiddgk
Copy link

how to find ipv6 in range.

@Lades268
Copy link

Great Job.

this modification can be made
if (strpos($range, '/' ) == false) { return $ip == $range; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment