Created
June 20, 2013 08:38
-
-
Save calvinchoy/5821191 to your computer and use it in GitHub Desktop.
PHP - ip and bigint converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Helper function to convert bigint to ip adress | |
function int2ip($intip){ | |
$ipVal = $intip; | |
$ipArr = array(0 => floor($ipVal/0x1000000) ); | |
$ipVint = $ipVal-($ipArr[0]*0x1000000); // for clarity | |
$ipArr[1] = ($ipVint & 0xFF0000) >> 16; | |
$ipArr[2] = ($ipVint & 0xFF00 ) >> 8; | |
$ipArr[3] = $ipVint & 0xFF; | |
$ipDotted = implode('.', $ipArr); | |
return $ipDotted; | |
} | |
//Helper function to convert ip adress to bigint | |
function ip2int($ip){ | |
$ipArr = explode('.',$ip); | |
$ip = $ipArr[0] * 0x1000000 | |
+ $ipArr[1] * 0x10000 | |
+ $ipArr[2] * 0x100 | |
+ $ipArr[3] | |
; | |
return $ip; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment