Explanation of mutual conversion between php ip addresses and integer types

Source: Internet
Author: User

Yesterday, to determine the source of the user's IP address, and then enter the address of the CIDR block where the user's IP address is located, we need to implement the function of converting the IP address and the integer type. Let's take a look.

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.