In PHP, how does one convert an IP address to a decimal number. In PHP, how do I convert an IP address to a decimal number? IP addresses are usually used in PHP, but these IP addresses are not in decimal format. In PHP, how does one convert an IP address in PHP to a decimal number? IP addresses are usually used in PHP, but these IP addresses are not in decimal format. In PHP, converting IP addresses to decimal numbers is a headache. the following two methods are simple to convert IP addresses to decimal numbers. Hope to help you.
Method 1:
The code is as follows:
Public function ipToLong (){
$ Ip = $ _ SERVER ['remote _ ADDR '];
$ Ip = explode ('.', $ ip );
$ Ip = array_reverse ($ ip); // array inversion
$ R = 0;
For ($ I = 0, $ j = count ($ ip); $ I <$ j; $ I ++ ){
$ R + = $ ip [$ I] * POWs (256, $ I );
}
$ R = sprintf ("% u", $ r );
Echo $ r;
}
Method 2:
The code is as follows:
Public function ipToLong (){
$ Ip = $ _ SERVER ['remote _ ADDR '];
$ Ip = explode ('.', $ ip );
$ R = ($ ip [0] <24) | ($ ip [1] <16) | ($ ip [2] <8) | $ ip [3];
If ($ r <0) $ r ++ = 4294967296;
Echo $ r;
}
Both results are 3232235877 on the local server, and the ip address used is 192.168.1.101. We can use ping 192.168.1.101 and ping 3232235877 to check whether they are the same.
Some may not know that mysql and php both provide the IP address conversion decimal number function.
MySQL provides the INET_ATON and INET_NTOA functions to convert between addresses and integers.
1. the IP address is converted from the dot-based format to the digital format.
The code is as follows:
Mysql> select inet_aton ('2017. 0.0.1 ');
+ ------------------------ +
| Inet_aton ('2017. 0.0.1 ') |
+ ------------------------ +
| 1, 2130706433 |
+ ------------------------ +
1 row in set (0.00 sec)
2. the IP address is converted from the digital format to the dot-division format.
The code is as follows:
Mysql> select inet_ntoa (2130706433 );
+ ----------------------- +
| Inet_ntoa (2130706433) |
+ ----------------------- +
| 127.0.0.1 |
+ ----------------------- +
1 row in set (0.00 sec)
Php can directly use the ip2long function.
The code is as follows:
Echo ip2long ('192. 168.1.38 ');
Output: 3232235814
How do I convert an IP address to a decimal number in the http://www.bkjia.com/PHPjc/371387.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371387.htmlTechArticlePHP? IP addresses are usually used in PHP, but these IP addresses are not in decimal format. In PHP, how do I set the IP address...