Explanation of mutual conversion between IP addresses and integer numbers in PHP

Source: Internet
Author: User
This article describes how to convert IP addresses and integer numbers in PHP. This article introduces the use of ip2long and long2ip functions in PHP and their bugs, finally, two algorithms are provided by myself. if you need them, you can refer to converting IP addresses to integer storage. this is a major trend in database optimization. many people are still using string storage to store IP addresses, string indexes consume a lot of resources than integer indexes, especially when the data volume in the table is large and you want to query the data of an ip segment. the ip address mentioned today refers to ip4, and ip6 is not in the scope of this article.

System functions ip2long and long2ip
The built-in function ip2long in PHP can convert IP addresses to integer types.

The code is as follows:


$ Ip = '1970. 110.11.49 ';
Echo ip2long ($ ip );


Output:

The code is as follows:


-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 a non-signed one, you can write it like this:

The code is as follows:


3530427185

Use long2ip to convert integer data back to IP address

The code is as follows:


$ Ip = '1970. 110.11.49 ';
$ Ip_int = ip2long ($ ip );
Echo $ ip ."
";
Echo $ ip_int ."
";
Echo long2ip ($ ip_int );


Output:

The code is as follows:


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:


$ Ip = '1970. 110.011.49 ';
$ Ip_int = ip2long ($ ip );
Echo $ ip ."
";
Echo $ ip_int ."
";
Echo long2ip ($ ip_int );


Output:

The code is as follows:


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:


$ Ip = '021. 110.11.49 ';
$ Ip_int = ip2long ($ ip );
Echo $ ip ."
";
Echo $ ip_int ."
";
Echo long2ip ($ ip_int );


Output:

The code is as follows:


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, and 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:


$ Ip = '1970. 110.11.49 ';

Function ipToInt ($ ip ){
$ Iparr = explode ('.', $ ip );
$ Num = 0;
For ($ I = 0; $ I $ Num + = intval ($ iparr [$ I]) * pow (256, count ($ iparr)-($ I + 1 ));
}
Return $ num;
}

Echo $ ip .'
';
$ Ip_int = ipToInt ($ ip );
Echo $ ip_int .'
';
Echo long2ip ($ ip_int );


Output:

The code is as follows:


0210.110.11.49
3530427185
210.110.11.49

2. bitwise operators

The code is as follows:


$ 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 .'
';
$ Ip_int = ipToInt ($ ip );
Echo $ ip_int .'
';
Echo long2ip ($ ip_int );


Output:

The code is as follows:


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:


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'
';
Echo '1970. 205.11.49 ,';
Var_dump (check_ip ('192. 205.11.49 '));
[Code]

Output:
[Code]
210.285.11.49, bool (false)
210.205.11.49, bool (true)


Second, use ip2long to return

The code is as follows:


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'
';
Echo '1970. 205.11.49 ,';
Var_dump (check_ip ('192. 205.11.49 '));


Output:

The code is as follows:


210.285.11.49, bool (false)
210.205.11.49, bool (true)

Postscript

Many users use ip2long to convert ip address writing databases and store int fields. However, the ip2long function has different values on different system platforms, therefore, the ip address obtained by long2ip does not match the original ip address when the ip address is reversed from the database.
If it is mysql, you can use the mysql system functions INET_ATON and INET_NTOA, or use the bigint type for processing, or write the function by yourself.

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.