Translation of IP addresses and integer numbers in PHP

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

This article mainly introduces the IP address and integer number in PHP, the use of PHP function Ip2long and Long2ip, as well as their bug introduction, finally give their own written two algorithms, the need for friends can refer to the next

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.

The code is as follows:

$ip = ' 210.110.11.49 '; Echo Ip2long ($ip);

Output:

-764540111

The integral type of the output has a minus sign because we get the result is signed integer, signed integer maximum 2147483647, to convert the result to unsigned can be so written

3530427185

Convert an integer to an IP address using LONG2IP

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

Output:

210.110.011.49-764540623210.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

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

Output:

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

$ip= ' 0210.110.11.49 '; functionIptoint ($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:

0210.110.11.493530427185210.110.11.49

Second, through bitwise operators

$ip= ' 0210.110.11.49 '; functionIptoint ($ip){    $iparr=Explode(‘.‘,$ip); return(intval($iparr[0]<<24)] | (intval($iparr[1]) &LT;&LT;16) | (intval($iparr[2]) &LT;&LT;8) | (intval($iparr[3]));} Echo  $ip.‘ <br/> ';$ip _int= Iptoint ($ip);Echo $ip _int.‘ <br/> ';Echo Long2ip($ip _int);

Output:

0210.110.11.49-764540111210.110.11.49

Detect if IP is legitimate

First, self-traversal detection

functionCHECK_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

 function  check_ip ( $ip   if  (ip2long  ( $ip )) return  true  ;  return  false  ;}  echo  ' 210.285.11.49, '  echo  ' <br/> ' ;  echo  ' 210.205.11.49, ' 

Output:

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.