PHP IP address and integer digital conversion detailed _php tips

Source: Internet
Author: User
Tags bitwise operators

IP conversion into integer storage is a major trend of database optimization, many people are still using the storage of IP string type storage, the string index than the whole index consumes a lot of resources, especially in the table of large amount of data, as well as the query for an IP segment of the data, today said IP refers to IP4,IP6 is not within the scope of this article.

system function Ip2long and LONG2IP
PHP has built-in functions Ip2long can convert an IP address to an integral type.

Copy Code code as follows:

$ip = ' 210.110.11.49 ';
echo Ip2long ($IP);

Output:
Copy Code code as follows:

-764540111

The integer of the output has a minus sign because the result we get is a signed integer, a signed integer maximum of 2147483647, to convert the result to an unsigned type can be written like this:
Copy Code code as follows:

3530427185

Convert an integer back to an IP address using LONG2IP

Copy Code code as follows:

$ip = ' 210.110.11.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy Code code as follows:

210.110.11.49
-764540111
210.110.11.49

As you can see from the results, the IP and integer can be completed through functions.

system function Small Bug

This bug on the Internet a search is, to the effect that the IP section plus a leading 0, first look at this bug example

Copy Code code as follows:

$ip = ' 210.110.011.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy Code code as follows:

210.110.011.49
-764540623
210.110.9.49

The conversion result does not match, we try to add a leading 0 before the IP first digit, then see

Copy Code code as follows:

$ip = ' 021.110.11.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy Code code as follows:

021.110.11.49
292424497
17.110.11.49

There was an error in the conversion result. The above examples are due to the addition of leading 0 after the result of the conversion error, and the reverse result and the original conversion IP mismatch.

Conversion principle

There are currently two algorithms:
The first, the first is multiplied by the three times 256, the second is multiplied by the square of 256, the third is multiplied by 256, the final sum

Copy Code code as follows:

$ip = ' 0210.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:
Copy Code code as follows:

0210.110.11.49
3530427185
210.110.11.49

Second, by bitwise operators

Copy Code code as follows:

$ip = ' 0210.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:
Copy Code code as follows:

0210.110.11.49
-764540111
210.110.11.49

Detect IP is legal

First, their own traverse detection

Copy Code code as follows:

function Check_ip ($IP) {
$iparr = Explode ('. ', $IP);
foreach ($iparr as $v) {if ($v >255) return false;}
return true;
}

Echo ' 210.285.11.49, ';
Var_dump (check_ip (' 210.285.11.49 '));
echo ' <br/> ';
Echo ' 210.205.11.49, ';
Var_dump (check_ip (' 210.205.11.49 '));
[Code]

Output:
[Code]
210.285.11.49,bool (False)
210.205.11.49,bool (True)


second, use Ip2long return
Copy Code code as follows:

function Check_ip ($IP) {
if (Ip2long ($IP)) return true;
return false;
}

Echo ' 210.285.11.49, ';
Var_dump (check_ip (' 210.285.11.49 '));
echo ' <br/> ';
Echo ' 210.205.11.49, ';
Var_dump (check_ip (' 210.205.11.49 '));

Output:
Copy Code code as follows:

210.285.11.49,bool (False)
210.205.11.49,bool (True)

Postscript

Many people put the IP write library with ip2long conversion of the type of int, but, on different system platforms, Ip2long function to get the value is different, so may cause in the data read from the database to reverse IP long2ip IP and the original IP does not conform to
If it is MySQL can use the MySQL system function Inet_aton and Inet_ntoa solve, or use bigint type processing, or write the function themselves.

Related Article

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.