Skip to content

Instantly share code, notes, and snippets.

@calvinchoy
Created June 20, 2013 08:38
Show Gist options
  • Save calvinchoy/5821191 to your computer and use it in GitHub Desktop.
Save calvinchoy/5821191 to your computer and use it in GitHub Desktop.
PHP - ip and bigint converter
<?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