How do you turn IP addresses into decimal numbers in PHP? There are many times in PHP that use IP addresses, but this IP address is not 10 binary. Then how to translate the IP address into decimal number in PHP is our headache, the following two methods are my processing to a relatively simple IP address to the decimal number method. Hope to be helpful to everyone.
Method One:
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] * POW ($i);
}
$r = sprintf ("%u", $r);
Echo $r;
}
Method Two:
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;
}
Two results in the local server, the result is 3232235877, the IP used is 192.168.1.101. We tested it with ping 192.168.1.101 and ping 3232235877来 to see if it was the same.
Maybe some friends don't know. MySQL and PHP are provided with the IP conversion decimal number function
MySQL provides Inet_aton and Inet_ntoa two functions for converting between addresses and integers.
1. IP is converted to digital format by dot format.
The code is as follows
Mysql> Select Inet_aton (' 127.0.0.1 ');
+------------------------+
| Inet_aton (' 127.0.0.1 ') |
+------------------------+
| 2130706433 |
+------------------------+
1 row in Set (0.00 sec)
2. IP is converted from digital format to point 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
http://www.bkjia.com/PHPjc/371387.html www.bkjia.com true http://www.bkjia.com/PHPjc/371387.html techarticle How do you turn IP addresses into decimal numbers in PHP? There are many times in PHP that use IP addresses, but this IP address is not 10 binary. So how will the IP address in PHP ...