Paul Wolstenholme wrote:

> Greetings,
> 
> I'd like to check to see if an ip is within an ip cidr style network
> block (192.75.242.157 with 192.75.242.0/23). Initially, I started off
> with some code derived from some perl code that I found (code below).
> However, the bitmask calcution did not work ( $bitmask = 0xffffffff <<
> (32 - $nbits);) and the hexdec did not appear to support values as bit
> as  0xffffffff.

> ip = -1068764584
> start = -1068764672
> mask = -16777216

Okay, basically what's going on is that PHP is using SIGNED integers, and 
that's messing you up, almost for sure.

Since your actual problem is to work with 3/4ths of the IP that has to 
match exactly, and only the last section needs the bit-math, what *I* would 
do is to http://php.net/explode the IPs, and do the bit-shifting on the 4th 
byte, and use == on the other 3 bytes.

You then do not need to worry about signed/unsigned integers and 
wrap-around arithmetic.

PHP's bit-shift operators *DO* work properly, and if you interpret the 
negative numbers correctly, they *ARE* the right bits for an unsigned 
integer, but that's too hard for my poor little brain, and doing it with 
explode and bit-shifting on the last byte is far more understandable.

-- 
Like music?  http://l-i-e.com/artists.htm


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to