note 51660 modified in function.ip2long by danbrown

From: Date: Fri, 05 Dec 2008 13:46:26 +0000
Subject: note 51660 modified in function.ip2long by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147705@lists.php.net to get a copy of this message
Here is a function that tells you if an ip address is in a CIDR range. However, the CIDR argument can be an array of CIDRs. This was created from other matchCIDR functions in the user notes. <?php function matchCIDR($addr, $cidr) { // $addr should be an ip address in the format '0.0.0.0' // $cidr should be a string in the format '100/8' // or an array where each element is in the above format $output = false; if ( is_array($cidr) ) { foreach ( $cidr as $cidrlet ) { if ( matchCIDR( $addr, $cidrlet) ) { $output = true; } } } else { list($ip, $mask) = explode('/', $cidr); $mask = 0xffffffff << (32 - $mask); $output = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); } return $output; } ?> --was-- Here is a function that tells you if an ip address is in a CIDR range. However, the CIDR argument can be an array of CIDRs. This was created from other matchCIDR functions in the user notes. function matchCIDR($addr, $cidr) { // $addr should be an ip address in the format '0.0.0.0' // $cidr should be a string in the format '100/8' // or an array where each element is in the above format $output = false; if ( is_array($cidr) ) { foreach ( $cidr as $cidrlet ) { if ( matchCIDR( $addr, $cidrlet) ) { $output = true; } } } else { list($ip, $mask) = explode('/', $cidr); $mask = 0xffffffff << (32 - $mask); $output = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); } return $output; } http://php.net/manual/en/function.ip2long.php

« previous php.notes (#147705) next »