Home  >  Article  >  Backend Development  >  Solution to negative numbers when php uses ip2long

Solution to negative numbers when php uses ip2long

*文
*文Original
2018-01-02 17:16:001819browse

How to solve the problem of negative numbers when using ip2long in php? This article mainly introduces the reasons and solutions for negative numbers in php ip2long. Share it with everyone and give it as a reference. I hope to be helpful.

php provides ip2long and long2ip methods to process ip addresses.

1. ip2long - Convert an IPV4 string Internet protocol into a digital format

int ip2long ( string $ip_address )

Parameters: ip_address An address in a standard format.

Return value: Returns the converted number of the IP address or FALSE if ip_address is invalid.

2. long2ip - Convert the number format into an IPV4 string Internet protocol

string long2ip (string $proper_address)

Parameters: proper_address The correct address representation of long integer.

Return value: Returns the Internet address as a string.

3. How to use

<?php
$ip = &#39;10.1.1.1&#39;;
$ip_long = ip2long($ip);
echo $ip_long.PHP_EOL; // 167837953
echo long2ip($ip_long); // 10.1.1.1
?>

4. Reasons for negative numbers and how to deal with them

When the ip address is relatively large, ip2long will have a negative number:

<?php
$ip = &#39;192.168.101.100&#39;;
$ip_long = ip2long($ip);
echo $ip_long.PHP_EOL; // -1062705820
echo long2ip($ip_long); // 192.168.101.100
?>

Reason explanation:

IPv4 uses unsigned 32 bit address, so there are at most 2 to the 32nd power minus 1 (4294967295) addresses. Write decimal numbers separated by 4 decimal points.

Remember as A.B.C.D, for example: 192.168.100.100.

Each decimal number in the IPv4 address is an unsigned byte, ranging from 0 to 255. Converting the IPv4 address to an unsigned number actually means placing each decimal number in the corresponding 8 bits, forming a 4-byte unsigned integer. 192.168.100.100, 192,168 in the high 8 digits and 100,100 in the low 8 digits.

C implementation example:

#include <stdio.h>

int main(int argc, char** argv)
{
  unsigned int ip_long = (192 << 24) | (168 << 16) | (100 << 8) | 100;
  printf("%u\n", ip_long);
  printf("%d\n", ip_long);

  return 0;
}

fdipzone@ubuntu:~/C$ gcc -o ip2long ip2long.c
fdipzone@ubuntu:~/C$ ./ip2long
3232261220
-1062706076

It can be seen that even if the ip_long declaration is an unsigned integer, %u still needs to be specified when outputting to format the output as an unsigned integer.

Because 192 is greater than 127 (binary is 01111111), 192 (8 bits) is expressed in binary, and the highest bit must be 1. As a result, the highest bit of this 4-byte integer is 1.

Although ip_long is defined as an unsigned integer, the printf method ignores the declaration. So you need to use %u formatting for output. If the highest bit is 0, just use %d.

Another example:

ip:112.24.55.99

#include <stdio.h>

int main(int argc, char** argv)
{
  unsigned int ip_long = (112 << 24) | (24 << 16) | (55 << 8) | 99;
  printf("%u\n", ip_long);
  printf("%d\n", ip_long);

  return 0;
}
fdipzone@ubuntu:~/C$ gcc -o ip2long ip2long.c
fdipzone@ubuntu:~/C$ ./ip2long
1880635235
1880635235

Solution:

Use %u to format unsigned integers when outputting.

<?php
$ip = &#39;192.168.101.100&#39;;
$ip_long = sprintf(&#39;%u&#39;,ip2long($ip));
echo $ip_long.PHP_EOL; // 3232261476 
echo long2ip($ip_long); // 192.168.101.100
?>

Related recommendations:

How does PHP get the real IP of the user client

PHP implements salted image encryption

How PHP handles MySQL dead connections

The above is the detailed content of Solution to negative numbers when php uses ip2long. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn