Home >Backend Development >PHP Tutorial >PHP code to implement IP address and number exchange
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PHP中文网</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <?php /* * 作者:XXXX */ //将IP转换为数字 function ipton($ip) { $ip_arr=explode('.',$ip);//分隔ip段 foreach ($ip_arr as $value) { $iphex=dechex($value);//将每段ip转换成16进制 if(strlen($iphex)<2)//255的16进制表示是ff,所以每段ip的16进制长度不会超过2 { $iphex='0'.$iphex;//如果转换后的16进制数长度小于2,在其前面加一个0 //没有长度为2,且第一位是0的16进制表示,这是为了在将数字转换成ip时,好处理 } $ipstr.=$iphex;//将四段IP的16进制数连接起来,得到一个16进制字符串,长度为8 } return hexdec($ipstr);//将16进制字符串转换成10进制,得到ip的数字表示 } //将数字转换为IP,进行上面函数的逆向过程 function ntoip($n) { $iphex=dechex($n);//将10进制数字转换成16进制 $len=strlen($iphex);//得到16进制字符串的长度 if(strlen($iphex)<8) { $iphex='0'.$iphex;//如果长度小于8,在最前面加0 $len=strlen($iphex); //重新得到16进制字符串的长度 } //这是因为ipton函数得到的16进制字符串,如果第一位为0,在转换成数字后,是不会显示的 //所以,如果长度小于8,肯定要把第一位的0加上去 //为什么一定是第一位的0呢,因为在ipton函数中,后面各段加的'0'都在中间,转换成数字后,不会消失 for($i=0,$j=0;$j<$len;$i=$i+1,$j=$j+2) {//循环截取16进制字符串,每次截取2个长度 $ippart=substr($iphex,$j,2);//得到每段IP所对应的16进制数 $fipart=substr($ippart,0,1);//截取16进制数的第一位 if($fipart=='0') {//如果第一位为0,说明原数只有1位 $ippart=substr($ippart,1,1);//将0截取掉 } $ip[]=hexdec($ippart);//将每段16进制数转换成对应的10进制数,即IP各段的值 } $ip = array_reverse($ip); return implode('.', $ip);//连接各段,返回原IP值 } echo ipton('119.255.31.226'); echo '<br>'; $num='379374783'; echo strlen($num).'<br/>'; echo ntoip($num).'<br/>'; echo 'trueipnum:'.ip2long('119.255.31.226').'<br/>'; echo 'trueip:'.long2ip('3793747831'); ?> </body> </html>
mysql comes with the inet_aton function to convert the ip address into a number, and the inet_ntoa function to convert the number into an ip.
SELECT INET_ATON('10.122.22.1')
The above conversion function is original by the webmaster. It happens to achieve the same conversion effect as the mysql system function. Haha, I am excited for a long time~~
It turns out that PHP provides ip2long and long2ip2 Function to realize the conversion of IP and numbers, the webmaster has done a useless job, but it should be exercised logical thinking~~
These are two implementation methods, one is to implement it programmatically; the other is to implement it directly in the sql statement , very convenient and good, I saved it and posted it to my blog, so I won’t forget it in the future.
When saving IP data in the database (MySQL), we are used to using the ip2long function to generate an integer and then store 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) Save the result of using ip2long to process an ip address. 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 write it Because the database 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, and " 127.255.255.255″This IP address.
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 a bigint type, so that it can be saved on a 64-bit machine. It is 3395898803, and you can still get the correct result using the long2ip function.