Comments Feed

Posts Tagged ‘ip2long’

Aug
16

ip2long vs INET_ATON

Whilst I was making the IP range function for my crawler catcher, I stumbled upon the problem of signed and unsigned integers for ip address (ip2long, INET_ATON) and back again (long2ip, INET_NTOA) using PHP and MySQL respectively. This caused me a problem when I used ip2long to sort ip addresses by order, and received negative numbers (-16843010) this gave me some funny results when using php's sort() function. I was getting 1 IP out of a range appearing 3/4 numbers out of place.

Upon futher inspection I found out that php uses signed integers (positive and negative numbers). This messes with the array sort functon. After I changed to unsigned integers, my ip ranges worked perfectly.

MySQL also won't produce an IP from a negative integer, and just returns false:

I've provided some demonstrations, and a php function to produce an unsigned long from an IP address.

Read the rest of this entry »

posted by Mark Willis