IP address acquisition and conversion, IP address acquisition and conversion

Source: Internet
Author: User

IP address acquisition and conversion, IP address acquisition and conversion
IP address acquisition and conversion

1. Preface

Converting an IP address to an integer storage is a major trend in database optimization. String indexes consume a lot of resources than integer indexes, especially when the data volume in a table is large, and query the data of an ip segment. The IP address mentioned in this article is ip4, and ip6 is no longer in scope

2. ip4 is converted to an integer

Here we will introduce:

  • Php built-in function ip2long
  • Php native ip2long simulation process

2.1, ip2long

Determines whether an ip4 address is a valid ip address. If yes, a long integer is returned. If not, false is returned.

The following is a test:

However, when the value of ip4 is large, ip2long conversion may result in a negative number.

2.2 native php ip2long simulation process

Although php's built-in ip2long is easy to use, because the returned value is int, it will cause data overflow. For example, some IP addresses are converted to negative numbers. In order to look good, you still need to simulate an ip2long process by yourself, not to mention, add the code first

Function ipToInt ($ ip) {$ newHex = ''; $ aIp = explode ('. ', $ ip); // divide the ip address into an array foreach ($ aIp as $ key => $ value) {// The maximum value in decimal format is 255, which exceeds the valid ip address, directly return if ($ value> 255) {return '';} $ hex = dechex ($ value ); // convert decimal to hexadecimal format // The maximum value of each ip segment is 255, and The hexadecimal value is FF, so the maximum value is two, // For example, IP = if 0 is not supplemented, the hexadecimal format is 1111, And the decimal value is 4369. // The hexadecimal value of 0 is 01010101, decimal 16843009 if (strlen ($ hex) <2) {$ hex = '0 '. $ hex; // If the hexadecimal length is less than 2, the system automatically fills in 0} $ newHex. = $ hex;} $ int = hexdec ($ newHex); // convert the hexadecimal format to return $ int ;}

The following is a test:

3. convert an integer to an ip address

Here we will introduce:

  • Php built-in function long2ip
  • Php native long2ip simulation process
3.1 long2ip

Long2ip only returns to,

If the pass-by value is null, is returned by default. If the maximum value is exceeded, is returned.

The following is a test:

Long2ip can identify the negative number generated by ip2long conversion (which is gratifying). during use, ip2long and long2ip can be used together, of course, we also need to introduce how to use native to simulate the long2ip process.

3.2 native php long2ip simulation process

The long2ip provided by php can solve most of the problems. Here we will briefly introduce the native simulation method.

Function intToIp ($ int) {// The maximum FFFFFF value is 4294967295 $ int = $ int> 4294967295? 4294967295: $ int; $ dec = dechex ($ int); // convert decimal to hexadecimal. // The leftmost 0 is ignored by default. After all, it is 0, it's useless to keep the value 0. // However, the value 0 in the middle will be retained, and the maximum IP hexadecimal value is FFFFFF (Large F group) // to prevent the emergence of a 7-bit IP address, we can only manually add 0 in pairs (two pairs) if (strlen ($ dec) <8) {$ dec = '0 '. $ dec; // If the length is less than 8, 0 is automatically filled.} for ($ I = 0; $ I <8; $ I + = 2) {$ hex = substr ($ dec, $ I, 2); $ ippart = substr ($ hex, 0, 1 ); // intercept the first hexadecimal if ($ ippart = '0') {$ hex = substr ($ hex, 1, 1); // if the first digit is 0, it indicates that the original value is only one digit, but it still needs to be split} $ aIp [] = hexdec ($ hex); // convert the hexadecimal number of each segment to my decimal number, that is, the value of each ip segment} return implode ('. ', $ aIp );}
The following is a test:

4.1, ip2long

Determines whether an ip4 address is a valid ip address. If yes, a long integer is returned. If not, false is returned.

Http://php.net/manual/en/function.ip2long.php for details

4.2. dechex

Convert decimal to hexadecimal (do not use your own writing method, nice)

Http://www.php.net/manual/zh/function.dechex.php for details

4.3. hexdec

Hexadecimal to decimal

Http://php.net/manual/zh/function.hexdec.php for details

4.4 long2ip

Converts a long integer to a string in the ip4 format to determine whether an ip address is valid.

Http://php.net/manual/en/function.long2ip.php for details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.