One, MySQL's inet_aton:ip->num,inet_ntoa:number->ip.
1, create a table
CREATE TABLE Iplog (
IP int (ten) unsigned DEFAULT NULL,
Name Char (TEN) DEFAULT NULL
)
2, insert IP
INSERT into Iplog values (Inet_aton (10.0.0.1), ' Nwip ');
3, take IP
Select Inet_ntoa (IP) from Iplog;
SELECT * from iplog where IP = Inet_aton (' 10.0.0.1 ');
Two, PHP
Ip2long ()
Log2ip ()
Third, the original method
<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP/** XXXX*///convert IP to digitalfunctionIpton ($ip){ $ip _arr=Explode(‘.‘,$ip);//separating IP segments foreach($ip _arr as $value) { $iphex=Dechex($value);//convert each IP to 16 binary if(strlen($iphex) <2)//255 of the 16 binary representation is FF, so the 16 binary length of each IP will not exceed 2 { $iphex= ' 0 '.$iphex;//if the converted 16-digit length is less than 2, precede it with a 0//no length of 2, and the first bit is a 0-binary representation of 16, this is to handle when converting the number to IP. } $ipstr.=$iphex;//connect the 16-digit number of the four-segment IP to get a 16 binary string with a length of 8 } return Hexdec($ipstr);//converts a 16 binary string to 10, resulting in a digital representation of the IP} //converting a number to an IP for the reverse process of the above functionfunctionNtoip ($n){ $iphex=Dechex($n);//convert 10 binary numbers to 16 binary $len=strlen($iphex);//get the length of the 16 binary string if(strlen($iphex) <8) { $iphex= ' 0 '.$iphex;//If the length is less than 8, add 0 to the front $len=strlen($iphex);//re-get the length of the 16 binary string } //This is because the Ipton function to get the 16 binary string, if the first bit is 0, after conversion to a number, is not displayed//So, if the length is less than 8, it must be the first 0 plus//Why must be the first bit of 0, because in the Ipton function, the subsequent paragraphs add the ' 0 ' are in the middle, converted into numbers, won't disappear for($i=0,$j= 0;$j<$len;$i=$i+1,$j=$j+2) {//cycle through 16 binary strings, 2 lengths per intercept $ippart=substr($iphex,$j, 2);//get 16 binary numbers for each IP $fipart=substr($ippart, 0, 1);//intercept the first digit of the 16 binary number if($fipart= = ' 0 ') {//if the first digit is 0, it indicates that the original number is only 1 bits $ippart=substr($ippart, 1, 1);//intercept the 0 . } $ip[]=Hexdec($ippart);//converts each 16-digit number into a corresponding 10-digit number, which is the value of the IP segment } $ip=Array_reverse($ip); return implode(‘.‘,$ip);//connect each segment to return the original IP value}EchoIpton (' 119.255.31.226 ');Echo' <br> ';$num= ' 379374783 ';Echo strlen($num).‘ <br/> ';EchoNtoip ($num).‘ <br/> '; Echo' Trueipnum: '.Ip2long(' 119.255.31.226 '). ' <br/> ';Echo' Trueip: '.Long2ip(' 3793747831 ');?></body>We use an int (11) type (range-2147483648-2147483647) to save the result of processing an IP address with Ip2long,
For example, the IP is ' 202.105.77.179′ ', then the result on the 32-bit machine is:-899068493, and on the 64-bit machine is 3395898803. And then write it to the database, because the range of int (11) is exceeded, Therefore, the result on the 64-bit machine is saved as the maximum value of int (11): 2147483647. So when it is taken out of the database, it gets the wrong result and gets "127.255.255.255″ this IP address."
You can use the MySQL function: Inet_aton and INET_NTOA to handle the IP address, or the field to save the IP address to bigint type, so that the 64-bit machine is saved 3395898803, using the LONG2IP function can still get the correct results.
Thanks to the original author
MySQL Access IP method