How do you translate IP addresses into decimal numbers in PHP? Now there are many times in PHP to use the IP address, but this IP address is not the time to get 10 of the system. Then how to convert the IP address in PHP to decimal number is a headache for us, the following two ways I am finishing processing to relatively simple IP address to decimal number method. Hope to be helpful to everyone.
method One:
Copy Code code 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 (256, $i);
}
$r = sprintf ("%u", $r);
Echo $r;
}
Method Two:
Copy Code code 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 results are 3232235877, the use of IP is 192.168.1.101. We use ping 192.168.1.101 and ping 3232235877来 for testing to see if it is the same.