PHP IP Transformation (ip2long) of the detailed _php skills

Source: Internet
Author: User
Tags explode get ip pow

How do I convert four fields to an integer with a point-to-point IP network address protocol? PHP has such a function ip2long.

Copy Code code as follows:

<?php
Echo Ip2long ("10.2.1.3");
?>

we will get
167903491

How this is calculated, I now know that there are two algorithms. One
Copy Code code as follows:

<?php
function Ip2int ($IP) {
We first divide the IP into four paragraphs, $ip 1, $ip 2, $ip 3, $ip 4
List ($ip 1, $ip 2, $ip 3, $ip 4) =explode (".", $ip);
Then the first is multiplied by the three times 256, the second is multiplied by the 256 squared, and the third is multiplied by 256.
That's the value we got.
Return$ip1*pow (256,3) + $ip 2*pow (256,2) + $ip 3*256+ $ip 4;
}
?>

Second, using bit arithmetic
Copy Code code as follows:

<?php
function Ip2int ($IP) {
List ($ip 1, $ip 2, $ip 3, $ip 4) =explode (".", $ip);
Return ($ip 1<<24) | ($ip 2<<16) | ($ip 3<<8) | ($ip 4);
}
?>

We will find that some IPs are negative after being converted into integers, because the resulting result is a signed integer and the maximum value is 2147483647. To convert it to unsigned, you can use the
sprintf ("%u", Ip2long ($IP);

Can be converted to a positive integer. And the resulting results can be converted back to the original IP address using LONG2IP. You can also use Ip2long to verify whether an IP is valid, such as

Copy Code code as follows:

<?php
function Chk_ip ($IP) {
if (Ip2long ($ip) = = "-1") {
return false;
}
Returntrue;
}
Application
Var_export (Chk_ip ("10.111.149.42"));
Var_export (Chk_ip ("10.111.256.42"));
?>

will output true and False

When storing IP data in a database (MySQL), we are accustomed to using the Ip2long function to generate an integral type, which is then stored in a field of type int (11), but on different system platforms, the Ip2long function gets a different value, which can result in the reading of data from the database, Use Long2ip to get IP when the error occurred, say we encountered the situation:
We use an int (11) type (range-2147483648-2147483647) to save the result of processing an IP address with Ip2long, such as IP is ' 202.105.77.179′, then the result on a 32-bit machine is:- 899068493, and on the 64-bit machine, you get 3395898803. Then write it to the database, because it exceeds the range of int (11), so the result on the 64-bit machine is saved to the maximum value of int (11): 2147483647. So when you take it out of the database, Then get the wrong result and get "127.255.255.255″ this IP address."
There are many solutions, such as using MySQL's functions: Inet_aton and Inet_ntoa to handle IP addresses, or changing the field of an IP address to a bigint type, which saves 3395898803 on a 64-bit machine, You can still get the correct results using the LONG2IP function.

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.