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, 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.
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, 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:
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 2: 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.
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 ;}