IP address processing in PHP

韦小宝
Release: 2023-03-17 17:50:01
Original
2239 people have browsed it

In development, we always deal with ip addresses, and two functions are commonly used. .php provides the ip2long and long2ip methods for processing IP addresses. Today we will study it in detail. Let’s take a look at the ip processing method in PHP. Without further ado, let’s take a look at the ip address processing method in PHP!

In development, we always deal with IP addresses, and two functions are commonly used. PHP provides the ip2long and long2ip methods to process IP addresses.

ip2long converts an IPV4 string Internet protocol into a digital format

int ip2long ( string $ip_address )
//参数: ip_address 一个标准格式的地址。
//返回值: 返回IP地址转换后的数字 或 FALSE
Copy after login

long2ip converts a digital format into an IPV4 String Internet Protocol

string long2ip ( string $proper_address )
//参数: proper_address 长整型的正确地址表示。
//返回值: 返回互联网地址作为字符串。
Copy after login

When storing the IP address in the database, you can use the ip2long function to convert the IP address from a string to a long type to save storage space, and then use long2ip to restore the IP address when taking it out. After conversion, it is more convenient to judge whether the IP address is in the range, as follows:

$a=ip2long('127.0.0.66');
$b=ip2long('127.0.0.111');
$c=ip2long('127.0.0.10');
if($c>=$a && $c <=$b){
echo &#39;呜啦啦&#39;;
}else{
echo &#39;啦啦呜&#39;;
}
Copy after login

But you need to pay attention! ! ! Note Note Note

#The function ip2long() in php that converts IP into an integer is prone to problems. When the IP is relatively large, it will become a negative number.

Why? The answer is here!

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

is recorded 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 converting each decimal number. The system number is placed on the corresponding 8 bits to form a 4-byte unsigned integer. 192.168.100.100, 192,168 in the high 8 digits and 100,100 in the low 8 digits.


As follows

<?php
$ip = "192.168.1.2";
$ip_n = ip2long($ip);
echo $ip_n;      //输出的是个负数 -1062731518
?>
Copy after login

Because the integer value converted from IP is too large and exceeds the range of the integer, it becomes a negative number.

Need to be written as $ip_n = bindec(decbin(ip2long($ip))); In this way, the unsigned integer number can be obtained, as follows

<?php
$ip = "192.168.1.2";
$ip_n = bindec(decbin(ip2long($ip)));
echo $ip_n;      //得到 3232235778
?>
Copy after login

There is also a solution:

Use %u to format unsigned integer 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
?>
Copy after login

Related recommendations:

php ip2long causes negative numbers and solutions

php ip address conversion integer, integer conversion address

php IP Get City API (Innocent IP Database)

The above is the detailed content of IP address processing in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template