A detailed _php tutorial on PHP IP conversion shaping (ip2long)

Source: Internet
Author: User
Tags mysql functions
How do I convert four fields with a point-separated IP address protocol to an integer? PHP has such a function ip2long.
Copy CodeThe code is as follows:
Echo Ip2long ("10.2.1.3");
?>

we will get
167903491

This is how to calculate, currently I know there are two algorithms. One
Copy CodeThe code is as follows:
function Ip2int ($IP) {
We'll divide the IP into four segments, $ip 1, $ip 2, $ip 3, $ip 4
List ($ip 1, $ip 2, $ip 3, $ip 4) =explode (".", $ip);
Then the first paragraph is multiplied by 256 of the three squares, the second is multiplied by 256 squared, the third segment multiplied by 256
That's the value we get.
Return$ip1*pow (256,3) + $ip 2*pow (256,2) + $ip 3*256+ $ip 4;
}
?>

Second, using bit arithmetic
Copy CodeThe code is as follows:
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 IP is converted to an integer, which is negative, because the resulting result is a signed integer, the maximum value is 2147483647. To convert it to unsigned, you can use
sprintf ("%u", Ip2long ($IP);

Can be converted to a positive integer. And the result can be converted back to the original IP address with Long2ip. You can also use Ip2long to verify that an IP is valid, such as
Copy the Code code as follows:
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 we keep the IP data in the database (MySQL), we are accustomed to use the Ip2long function to generate an integer type, and then store it in an int (11) Type field, but on different system platforms, the value of the Ip2long function is different, which may result in reading data from the database, Use LONG2IP to get the IP when the error, say what we encountered:
We use an int (11) type (range-2147483648-2147483647) to save the result of processing an IP address with Ip2long, for example, the IP is ' 202.105.77.179′ ', then the result on the 32-bit machine is:- 899068493, and 3395898803 on 64-bit machines. It is then written to the database, because it exceeds the range of int (11), so the result on the 64-bit machine is saved as the maximum value of int (11): 2147483647. So when it's taken out of the database, Get the wrong result and get "127.255.255.255″ this IP address."
There are a number of workarounds, such as MySQL functions: Inet_aton and Inet_ntoa to handle IP addresses, or changing the field of the saved IP address to bigint type, so that the 64-bit machine has 3395898803 saved. You can still get the correct results using the LONG2IP function.

http://www.bkjia.com/PHPjc/327465.html www.bkjia.com true http://www.bkjia.com/PHPjc/327465.html techarticle How do I convert four fields with a point-separated IP address protocol to an integer? PHP has such a function ip2long. For example, copy code code as follows: PHP echo Ip2long ("10.2.1.3");

  • 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.