[Conversion principle]: assume that the IP address is w. x. y. z, the formula for converting the IP address to an integer is: intIP = 256*256*256 * w + 256*256 * x + 256 * y + z
[PHP conversion]: the PHP conversion method is relatively simple. It has two built-in functions.
Int ip2long(String $ ip_address) // Convert the ip address to an integer.
String long2ip(String $ proper_address) // converts an integer value to an ip address [MySQL conversion]: compared with MsSQL, MySQL conversion is relatively simple. Like PHP, it also has two built-in functions.
Convert IP to integer type:
Select INET_ATON (IP address) integer value to IP address
Select INET_NTOA (integer value of IP)
One instance
1. Manual implementation
The code is as follows: |
Copy code |
Function ip2number ($ ip) {
$ T = explode ('.', $ ip ); $ X = 0; For ($ I = 0; $ I <4; $ I ++) { $ X = $ x * 256 + $ t [$ I]; } Return $ x; } Function number2ip ($ num) { $ T = $ num; $ A = array (); For ($ I = 0; $ I <4; $ I ++) { $ X = $ t % 256; If ($ x <0) $ x + = 256; Array_unshift ($ a, $ x ); $ T = intval ($ t/256 ); } Return implode ('.', $ ); } |