Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 ) );
}
@christophrumpel

This comment has been minimized.

Copy link

christophrumpel commented Oct 7, 2014

Nice one thx!

@lnx05

This comment has been minimized.

Copy link

lnx05 commented Oct 24, 2014

thank you very much!

@cbalavessov

This comment has been minimized.

Copy link

cbalavessov commented Mar 27, 2015

nice one, very appreciated!

@Fuitad

This comment has been minimized.

Copy link

Fuitad commented Apr 30, 2015

+1 Super nice! Thanks :D

@DanStevens

This comment has been minimized.

Copy link

DanStevens commented Jun 1, 2015

Does this work correctly on 32-bit systems?

@gklimm

This comment has been minimized.

Copy link

gklimm commented Oct 6, 2015

Thx!

@noinstance

This comment has been minimized.

Copy link

noinstance commented Oct 29, 2015

+1

@teliov

This comment has been minimized.

Copy link

teliov commented Nov 18, 2015

Dope!

@shrekuu

This comment has been minimized.

Copy link

shrekuu commented Dec 10, 2015

I have another one:

function CheckIpRange($ip, $min, $max) {
        return (ip2long($min); < ip2long($ip) && ip2long($ip) < ip2long($max));
  }  

reference:

@waitxd

This comment has been minimized.

Copy link

waitxd commented Jan 12, 2016

@phlyper

This comment has been minimized.

Copy link

phlyper commented Feb 29, 2016

good job :)

@beygi

This comment has been minimized.

Copy link

beygi commented Apr 15, 2016

life saving

@nafisakanwal

This comment has been minimized.

Copy link

nafisakanwal commented May 9, 2016

returning 1 means it this ip is withen the range.right ?

@BlakeGardner

This comment has been minimized.

Copy link

BlakeGardner commented Jul 26, 2016

This is awesome!

@phlbnks

This comment has been minimized.

Copy link

phlbnks commented Aug 3, 2016

Great function! Thanks!

@AlbinoDrought

This comment has been minimized.

Copy link

AlbinoDrought commented Aug 19, 2016

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

@peterquentin

This comment has been minimized.

Copy link

peterquentin commented Oct 21, 2016

@waitxd thnx for sharing, it's just the best :)

@blazeag

This comment has been minimized.

Copy link

blazeag commented Nov 22, 2016

Awesome, thank you very much!

@Komalbandi

This comment has been minimized.

Copy link

Komalbandi commented Feb 14, 2017

Thank you very much.

@trenshaw

This comment has been minimized.

Copy link

trenshaw commented May 29, 2017

/**
 * 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

This comment has been minimized.

Copy link

turkhero commented Jun 29, 2017

thanks @trenshaw

@jorgeolivares

This comment has been minimized.

Copy link

jorgeolivares commented Aug 16, 2017

Thanks!

@selva987

This comment has been minimized.

Copy link

selva987 commented Aug 28, 2017

Really useful, Thanks!

@phantommax

This comment has been minimized.

Copy link

phantommax commented Nov 3, 2017

Great! Saved me some priceless time!

@NiMeDia

This comment has been minimized.

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

This comment has been minimized.

Copy link

agustin-tiendanube commented Jan 19, 2018

Thank you!

@williammeneses

This comment has been minimized.

Copy link

williammeneses commented May 10, 2018

Very good. Thank you.

@Quix0r

This comment has been minimized.

Copy link

Quix0r commented Jul 12, 2018

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

@welllima88

This comment has been minimized.

Copy link

welllima88 commented Nov 15, 2018

THAANKSS MAN. very good!

@roman-syrota

This comment has been minimized.

Copy link

roman-syrota commented Dec 21, 2018

Thanks!

@hirschtec-ep

This comment has been minimized.

Copy link

hirschtec-ep commented Apr 25, 2019

thx - nice peace!

@EvoWebFrance

This comment has been minimized.

Copy link

EvoWebFrance commented May 7, 2020

Very nice... thank you for this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.