How to solve the negative number of PHP using Ip2long? This paper mainly introduces the reason and solution of the negative number of PHP Ip2long. Share to everybody, make a reference for everybody. We hope to help you.
PHP provides Ip2long and Long2ip methods for IP address processing.
1. ip2long-converts a IPV4 string Internet protocol into a digital format
int Ip2long (string $ip _address)
parameter: ip_address a standard format address.
return Value: returns the number of IP address conversions or FALSE if IP_Address is invalid.
2. long2ip-converts the number format into a IPV4 string Internet protocol
String Long2ip (String $proper _address)
parameter: proper_address the correct address representation of the long integer type.
return Value: returns the Internet address as a string.
3. How to use
<?php$ip = ' 10.1.1.1 '; $ip _long = Ip2long ($IP); Echo $ip _long. Php_eol; 167837953echo Long2ip ($ip _long); 10.1.1.1?>
4, the cause of negative causes and treatment methods
When the IP address is large, Ip2long will appear negative:
<?php$ip = ' 192.168.101.100 '; $ip _long = Ip2long ($IP); Echo $ip _long. Php_eol; -1062705820echo Long2ip ($ip _long); 192.168.101.100?>
Reason Description:
The IPV4 uses unsigned 32-bit addresses, so there are up to 2 32-time minus 1 (4294967295) addresses. Write a 10 binary number separated by 4 decimal points.
Recorded as A.B.C.D, for example: 192.168.100.100.
IPV4 address each 10 binary number is an unsigned byte, in the range of 0~255, the IPV4 address to an unsigned number, in fact, each 10 binary number is placed on the corresponding 8 bits, a 4-byte unsigned integer form. 192.168.100.100,192,168 in high 8 bits 100,100 in low 8 bits.
Examples of C implementations:
#include <stdio.h>int main (int argc, char** argv) { unsigned int ip_long = (192 << 24) | (168 << 16) | (<< 8) | ; printf ("%u\n", Ip_long); printf ("%d\n", Ip_long); return 0;} fdipzone@ubuntu:~/c$ gcc-o ip2long ip2long.cfdipzone@ubuntu:~/c$./ip2long3232261220-1062706076
As you can see, even if the Ip_long declaration is an unsigned integer, the output still needs to indicate%u to format the output as an unsigned integer.
Since 192 is greater than 127 (binary is 01111111), 192 (8 bits) is represented by binary, the highest bit must be 1. Causes the highest bit of this 4-byte integer to be 1.
Although the Ip_long is defined as an unsigned integer, the printf method ignores the declaration. Therefore, you need to use%u format to output. If the highest bit is 0, you can use%d.
Another example:
Ip:112.24.55.99#include <stdio.h>int Main (int argc, char** argv) { unsigned int ip_long = (<< 24) | (<< 16) | (<< 8) | ; printf ("%u\n", Ip_long); printf ("%d\n", Ip_long); return 0;} fdipzone@ubuntu:~/c$ gcc-o ip2long ip2long.cfdipzone@ubuntu:~/c$./ip2long18806352351880635235
Workaround:
The output is formatted as an unsigned integer with%u.
<?php$ip = ' 192.168.101.100 '; $ip _long = sprintf ('%u ', Ip2long ($IP)); Echo $ip _long. Php_eol; 3232261476 Echo long2ip ($ip _long); 192.168.101.100?>
Related recommendations:
How PHP obtains the user client's real IP
PHP implementation of salt-added image encryption
How PHP handles MySQL dead connections