Translation of IP addresses and integer numbers in PHP

Source: Internet
Author: User
Tags bitwise operators explode integer numbers

IP conversion to integer storage is a major trend in database optimization, many people are currently storing IP in the use of string type storage, the string index than the integer index consumes a lot of resources, especially when the data in the table is large, and to query the data of an IP segment, today said IP refers to IP4,IP6 is not within the scope of this article.

system functions Ip2long and Long2ip
PHP has built-in functions ip2long can convert IP addresses to integer types.

Copy CodeThe code is as follows:
$ip = ' 210.110.11.49 ';
echo Ip2long ($IP);

Output:
Copy CodeThe code is as follows:
-764540111

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

convert an integer to an IP address using LONG2IP

Copy CodeThe code is as follows:
$ip = ' 210.110.11.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy CodeThe code is as follows:
210.110.11.49
-764540111
210.110.11.49

as you can see from the results, the IP and integer types can be done through functions.

system function Small bug

This bug online A search is, to the effect that the IP segment plus a leading 0, first to see the bug instance

Copy CodeThe code is as follows:
$ip = ' 210.110.011.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy CodeThe code is as follows:
210.110.011.49
-764540623
210.110.9.49

The conversion results do not match, we try to add a leading 0 before the first digit of the IP, and then look at

Copy CodeThe code is as follows:
$ip = ' 021.110.11.49 ';
$ip _int = Ip2long ($IP);
echo $ip. " <br/> ";
echo $ip _int. " <br/> ";
Echo Long2ip ($ip _int);

Output:
Copy CodeThe code is as follows:
021.110.11.49
292424497
17.110.11.49

The result of the conversion was wrong. All of the above examples are caused by the addition of leading 0 resulting in an error in the conversion result, and the reversal result does not match the original conversion IP.

Conversion Principle

There are currently two algorithms:
first, the first paragraph multiplied by 256 of the three square, the second segment multiplied by 256 squared, the third paragraph multiplied by 256, the last sum

Copy CodeThe code is 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 CodeThe code is as follows:
0210.110.11.49
3530427185
210.110.11.49

second, through bitwise operators

Copy CodeThe code is 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 CodeThe code is as follows:
0210.110.11.49
-764540111
210.110.11.49

detect if IP is legitimate

first, self-traversal detection

Copy CodeThe code is 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 to return
Copy CodeThe code is 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 CodeThe code is as follows:
210.285.11.49,bool (False)
210.205.11.49,bool (True)

PostScript

Many people use the IP write library to ip2long the field of the int type, but on different system platforms, the value of the Ip2long function is different, so it may cause the IP to be long2ip with the original IP when the data is read from the database to reverse IP.
If it is MySQL you can use the MySQL system function Inet_aton with Inet_ntoa to solve, or use the bigint type processing, or write your own function.

Translation of IP addresses and integer numbers in PHP

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.