First, reason for demand
Ip address conversion into plastic storage is very important for database optimization. Many people store IP addresses as string types. When the amount of data is huge, it will seriously consume resources. Moreover, the speed of shaping index is much greater than that of string index, so it is necessary to use ip to shape storage, and then convert the shape back to ip when used. PHP's IP conversion, this article explains ipv4, ipv6 will not be explained.
Second, the system built-in functionThe function used to convert IP is: ip2long
Example:
$ip=” 60.6.205.138”; echo ip2long($ip);
The function used to transform IP is: long2ip
Example:
$ip=”60.6.205.138”; $ipInt=ip2long($ip); echo long2ip($ipInt);
Three , common problems
1. When the IP address is relatively large, the converted integer may exceed the maximum shaping range. The maximum value of a signed integer is 2147483647. When this value is exceeded, it becomes a negative number.
Solution: Write $ip = bindec(decbin(ip2long($ip)));
Example:$ip = "192.168.10.1": $ip= bindec(decbin(ip2long($ip))); echo $ip;
Description: The decbin function converts decimal to binary, and the bindec function converts binary to integer.
2. When some IP addresses have prefix 0, the conversion will go wrong.
Example:$ip = '021.110.11.49'; $ip_int = ip2long($ip); echo $ip."<br />"; echo $ip_int."<br />"; echo long2ip($ip_int);
When the ip is converted to integer, and then converted back to ip, an error will occur, and the ip address does not match the original one. The reason is that there is a leading 0.
Solution:
First, because the IP conversion shaping is based on: the first section is multiplied by the cube of 256, the second section is multiplied by the square of 256, the third section is multiplied by 256, and finally The result of the sum.$ip = '060.06.205.138'; function ipToInt($ip){ $iparr = explode('.',$ip); $num = 0; for($i=0;$i<count($iparr);$i++){ $num += intval($iparr[$i]) *pow(256,count($iparr)-($i+1)); } return $num; } echo $ip.'<br />';
Explanation:
intval converts the variable to an integer type
pow(4,2) returns 16, returns the yth power of x
Second, solve it through bitwise operators.
$ip = '060.06.205.138'; function ipToInt($ip){ $iparr = explode('.',$ip); return (intval($iparr[0]<<24))|(intval($iparr[1])<<16)|(intval($iparr[2])<<8)| (intval($iparr[3])); } echo $ip.'<br />'; $ipInt = ipToInt($ip); echo $ipInt.'<br />'; echo long2ip($ipInt);
The bitwise operator algorithm is simply to shift the current number to the left by three bits and multiply the current number by the cube of 2, and to shift the left bit by two bits and multiply by the square of 2. The right shift operation is to move a binary bit operand to the right by the specified number of bits. The shifted bits are discarded. The empty bits shifted out on the left are either filled with 0 or with the sign bit, depending on the machine.
If you don’t understand, just follow the first method.
This article ends here. If you have any questions, you can leave a message and we will discuss it together. I hope you can practice more and deepen your impression. Let’s work hard together!
The above has introduced ip2long-long2ip, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.