System functions ip2long and long2ip
The built-in function ip2long in PHP can convert IP addresses to integer types.
The code is as follows: |
Copy code |
$ Ip = '1970. 110.11.49 '; Echo ip2long ($ ip ); Output: -764540111 |
The output integer has a negative number because the result is a signed integer. The maximum value of a signed integer is 2147483647. To convert the result to an unsigned integer, you can write it like this.
3530427185
Use long2ip to convert integer data back to IP address
The code is as follows: |
Copy code |
$ Ip = '1970. 110.11.49 '; $ Ip_int = ip2long ($ ip ); Echo $ ip. "<br/> "; Echo $ ip_int. "<br/> "; Echo long2ip ($ ip_int ); Output:
210.110.11.49 -764540111 210.110.11.49 |
The result shows that ip and integer types can be completed through functions.
System function bug
This bug is found on the internet. The general idea is to add a leading 0 to an ip segment. Let's take a look at this bug instance.
The code is as follows: |
Copy code |
$ Ip = '1970. 110.011.49 ';
$ Ip_int = ip2long ($ ip ); Echo $ ip. "<br/> "; Echo $ ip_int. "<br/> "; Echo long2ip ($ ip_int ); Output:
210.110.011.49 -764540623 210.110.9.49 |
The conversion result does not match. We try to add the leading 0 before the first digit of the ip address.
The code is as follows: |
Copy code |
$ Ip = '021. 110.11.49 ';
$ Ip_int = ip2long ($ ip ); Echo $ ip. "<br/> "; Echo $ ip_int. "<br/> "; Echo long2ip ($ ip_int ); Output:
021.110.11.49 292424497 17.110.11.49 |
An error occurred while converting the result. In the preceding example, the conversion result is incorrect after the leading 0 is added. The result of the joint reversal does not match the original ip address.
Conversion principle
There are currently two algorithms:
1. Multiply the first section by the third power of 256, the second section by the square of 256, the third section by 256, and the last sum.
The code is as follows: |
Copy code |
$ Ip = '1970. 110.11.49 '; 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/> ';
$ Ip_int = ipToInt ($ ip ); Echo $ ip_int. '<br/> '; Echo long2ip ($ ip_int ); Output: 0210.110.11.49 3530427185 210.110.11.49 |
2. Bitwise operators
The code is as follows: |
Copy code |
$ Ip = '1970. 110.11.49 ';
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/> ';
$ Ip_int = ipToInt ($ ip ); Echo $ ip_int. '<br/> '; Echo long2ip ($ ip_int ); Output:
0210.110.11.49 -764540111 210.110.11.49 |
Check whether the IP address is valid
1. Self-traversal detection
The code is as follows: |
Copy code |
Function check_ip ($ ip ){
$ Iparr = explode ('.', $ ip ); Foreach ($ iparr as $ v) {if ($ v> 255) return false ;} Return true; } Echo '1970. 285.11.49 ,'; Var_dump (check_ip ('192. 285.11.49 ')); Echo '<br/> '; Echo '1970. 205.11.49 ,'; Var_dump (check_ip ('192. 205.11.49 ')); Output:
210.285.11.49, bool (false) 210.205.11.49, bool (true) |
Second, use ip2long to return
The code is as follows: |
Copy code |
Function check_ip ($ ip ){
If (ip2long ($ ip) return true; Return false; } Echo '1970. 285.11.49 ,';
Var_dump (check_ip ('192. 285.11.49 ')); Echo '<br/> '; Echo '1970. 205.11.49 ,'; Var_dump (check_ip ('192. 205.11.49 ')); Output:
210.285.11.49, bool (false) 210.205.11.49, bool (true) |