Detailed explanation of php IP conversion shaping (ip2long)_PHP tutorial

WBOY
Release: 2016-07-21 15:08:26
Original
1391 people have browsed it

How to convert an IP network address protocol address with four fields separated by dots into an integer? There is such a function ip2long in PHP. For example,

Copy code The code is as follows:

echo ip2long ("10.2.1.3");
?>

We will get
167903491

How is this calculated, currently I Know that there are two algorithms. One
Copy code The code is as follows:

function ip2int($ip){
//We first divide the ip into four segments, $ip1,$ip2,$ip3,$ip4
list($ip1,$ip2,$ip3,$ip4)=explode(".",$ip) ;
//Then the first segment is multiplied by 256 cubed, the second segment is multiplied by 256 squared, and the third segment is multiplied by 256
//This is the value we get
return$ip1 *pow(256,3)+$ip2*pow(256,2)+$ip3*256+$ip4;
}
?>

Second, use bits Operation
Copy code The code is as follows:

function ip2int($ip){
list($ip1,$ip2,$ip3,$ip4)=explode(".",$ip);
return($ip1<<24)|($ip2<<16)|($ip3< ;<8)|($ip4);
}
?>

We will find that some ips are negative after being converted into integers. This is because the The result is a signed integer, and the maximum value is 2147483647. To convert it to unsigned, you can use
sprintf("%u",ip2long($ip);

can be converted into a positive integer. And the result obtained can be converted back to the original IP address normally using long2ip. You can also use ip2long to verify whether an IP is valid, such as

Copy code The code is 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 saving IP data in a database (MySQL), we are used to using the ip2long function to generate an integer and then storing it in an int(11) type field. However, on different system platforms, the ip2long function obtains The values ​​are different, so it may cause errors when reading data from the database and using long2ip to get the IP. Let’s talk about the situation we encountered:
We use an int (11) type (range -2147483648 -2147483647 ) to save the result of processing an ip address with ip2long. For example, if the ip is '202.105.77.179', then the result obtained on a 32-bit machine is: -899068493, but on a 64-bit machine, 3395898803 is obtained. Then put it When writing to the database, since it exceeds the range of int(11), the result on the 64-bit machine is saved as the maximum value of int(11): 2147483647. So when it is taken out from the database, the wrong result is obtained. Get the IP address "127.255.255.255".
There are many solutions. For example, you can use mysql functions: INET_ATON and INET_NTOA to process the IP address; or change the field that saves the IP address to the bigint type, so that it can be used on a 64-bit machine. Although 3395898803 is saved, the correct result can still be obtained by using the long2ip function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327465.htmlTechArticleHow to convert an IP network address protocol address with four fields separated by dots into an integer? There is such a function ip2long in PHP. For example, copy the code. The code is as follows: ?php echo ip2long("10.2.1.3");...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template