IP addresses are usually used in PHP, but these IP addresses are not in decimal format. In PHP, how can I convert an IP address to a decimal number? The following describes how to convert an IP address to a decimal number in PHP? 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.