Home  >  Article  >  Backend Development  >  Detailed explanation of conversion between PHP IP address and integer_PHP tutorial

Detailed explanation of conversion between PHP IP address and integer_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:59708browse

Yesterday we had to determine the source of the user's IP, and then enter the address of the network segment where the user's IP is located. The first step we need to implement is to convert the IP address to an integer. Let me take a look at the

system function ip2long and long2ip

There is a built-in function ip2long in PHP that can convert an IP address to an integer.

The code is as follows Copy code
 代码如下 复制代码

$ip = '210.110.11.49';

echo ip2long($ip);

输出:

-764540111

$ip = '210.110.11.49';

echo ip2long($ip);

Output:

-764540111

The output integer type has a negative sign because the result we get is a signed integer type. The maximum value of the signed integer type is 2147483647. To convert the result to an unsigned type, we can write it like this
 代码如下 复制代码

$ip = '210.110.11.49';

$ip_int = ip2long($ip);

echo $ip."
";

echo $ip_int."
";

echo long2ip($ip_int);


输出:

210.110.11.49
-764540111
210.110.11.49

3530427185

Use long2ip to convert integer back to IP address

The code is as follows Copy code
 代码如下 复制代码


$ip = '210.110.011.49';

$ip_int = ip2long($ip);

echo $ip."
";

echo $ip_int."
";

echo long2ip($ip_int);


输出:

210.110.011.49
-764540623
210.110.9.49

$ip = '210.110.11.49';

$ip_int = ip2long($ip);
 代码如下 复制代码


$ip = '021.110.11.49';

$ip_int = ip2long($ip);

echo $ip."
";

echo $ip_int."
";

echo long2ip($ip_int);


输出:

021.110.11.49
292424497
17.110.11.49

echo $ip."
";

echo $ip_int."
";

echo long2ip($ip_int);


Output:

 代码如下 复制代码

$ip = '0210.110.11.49';


function ipToInt($ip){

$iparr = explode('.',$ip);

$num = 0;

for($i=0;$i

$num += intval($iparr[$i]) * pow(256,count($iparr)-($i+1));

}

return $num;

}


echo $ip.'
';

$ip_int = ipToInt($ip);

echo $ip_int.'
';

echo long2ip($ip_int);

 

输出:

0210.110.11.49
3530427185
210.110.11.49

210.110.11.49
-764540111
210.110.11.49
As you can see from the results, ip and integer types can be completed through functions. Small bug in system function You can find this bug all over the Internet. The general idea is to add a leading 0 to a certain segment of the IP address. Let’s take a look at an example of this bug first
The code is as follows Copy code

$ip = '210.110.011.49'; $ip_int = ip2long($ip); echo $ip."
"; echo $ip_int."
"; echo long2ip($ip_int);
Output: 210.110.011.49
-764540623
210.110.9.49
The conversion result does not match, let’s try adding a leading 0 before the first number of the ip, and look again
The code is as follows Copy code

$ip = '021.110.11.49'; $ip_int = ip2long($ip); echo $ip."
"; echo $ip_int."
"; echo long2ip($ip_int);
Output: 021.110.11.49
292424497
17.110.11.49
The conversion results are all wrong. The above examples are all due to the addition of leading 0s, resulting in errors in the conversion results, and the reversal results do not match the original converted IP Conversion Principle There are currently two algorithms: The first and first paragraph are multiplied by 256 cubed, the second paragraph is multiplied by 256 squared, the third paragraph is multiplied by 256, and the final sum is
The code is as follows Copy code
$ip = '0210.110.11.49';
function ipToInt($ip){ $iparr = explode('.',$ip); $num = 0; for($i=0;$i echo $ip.'
'; $ip_int = ipToInt($ip); echo $ip_int.'
'; echo long2ip($ip_int); Output: 0210.110.11.49
3530427185
210.110.11.49

第二、通过位运算符

return (intval($iparr[0]<<24))|(intval($iparr[1])<<16)|(intval($iparr[2])<<8)| (intval($iparr[3]));
 代码如下
 代码如下 复制代码


$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.'
';

$ip_int = ipToInt($ip);

echo $ip_int.'
';

echo long2ip($ip_int);


输出:

0210.110.11.49
-764540111
210.110.11.49

复制代码


$ip = '0210.110.11.49';

 代码如下 复制代码


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 '
';

echo '210.205.11.49,';

var_dump(check_ip('210.205.11.49'));


输出:

210.285.11.49,bool(false)
210.205.11.49,bool(true)


function ipToInt($ip){

$iparr = explode('.',$ip);
 代码如下 复制代码


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 '
';

echo '210.205.11.49,';

var_dump(check_ip('210.205.11.49'));


输出:

210.285.11.49,bool(false)
210.205.11.49,bool(true)

}


echo $ip.'
';

echo $ip_int.'
';
echo long2ip($ip_int);
输出:
0210.110.11.49
-764540111
210.110.11.49
检测IP是否合法
第一、自己遍历检测
 代码如下 复制代码

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 '
'; echo '210.205.11.49,'; var_dump(check_ip('210.205.11.49'));
输出: 210.285.11.49,bool(false)
210.205.11.49,bool(true) 第二、使用ip2long返回
 代码如下 复制代码

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 '
'; echo '210.205.11.49,'; var_dump(check_ip('210.205.11.49'));
输出: 210.285.11.49,bool(false)
210.205.11.49,bool(true) http://www.bkjia.com/PHPjc/631547.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631547.htmlTechArticle昨天要判断用户IP来源,然后再输入用户IP所在网段所在地址,我们第一条就需要把IP地址与整型互相转换功能实现,下面我来看看 系统函数...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn