Php converts IP addresses to integer instances. It is a common practice to convert IP addresses to integer data and save them to the database, the IP address conversion algorithm is intIP256 * 256*256 * w + 256*256 * x + 256 * y + z, next, convert IP addresses to integer data and save them to the database. this is a common practice, the IP address conversion algorithm is intIP = 256*256*256 * w + 256*256 * x + 256 * y + z. The following describes the specific instance.
[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
-
Manual implementation
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('.', $a); }
Fill = 256*256*256 * w + 256*256 * x + 256 * y + z. The following is a list...