Php code for IP address and digital interchange

Source: Internet
Author: User
Php code for IP address and digital interchange
PHP Chinese network
 
 '; $ Num = '000000'; echo strlen ($ num ).'
'; Echo ntoip ($ num ).'
'; Echo 'trueipnum:'. ip2long ('2017. large 31.226 ').'
'; Echo 'trueip:'. long2ip ('20140901');?>

Mysql comes with the inet_aton function to convert ip addresses to numbers. the inet_ntoa function converts numbers to ip addresses.

SELECT INET_ATON ('10. 122.22.1 ')

The above conversion function is original to the webmaster and happens to achieve the same conversion effect as the mysql system function ~~
In the past, PHP provided ip2long and long2ip2 functions to convert IP addresses and numbers. the webmaster did nothing, but exercise the right to think logically ~~
This is two implementation methods, one is implemented on the program; the other is implemented directly in the SQL statement, which is very convenient, good, saved, and sent to your blog, I will not forget it in the future.


When we save ip data in the database (MySQL), we are used to using the ip2long function to generate an integer and store it in an int (11) field. However, the ip2long function has different values on different system platforms, which may cause errors when reading data from the database and obtaining ip addresses using long2ip, let's talk about our situation:
We use an int (11) type (range:-2147483648-2147483647) to save the result of ip2long processing for an ip address. for example, the ip address is '123. 105.77.179', the result on the 32-bit machine is-899068493, but the result is 3395898803 on the 64-bit machine. then it is written to the database. because it exceeds the int (11) range, the result on the 64-bit machine is saved as the maximum value of int (11): 2147483647. so when we retrieve the IP address from the database, we get the error result and get the IP address "127.255.255.255.
There are many solutions. for example, you can use mysql functions: INET_ATON and INET_NTOA to process IP addresses, or change the field storing IP addresses to the bigint type, in this way, although 3395898803 is saved on a 64-bit machine, the long2ip function can still get the correct result.

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.