note 82013 added to function.ip2long
| From: | johniskew at yahoo dot com | Date: | Mon, 24 Mar 2008 18:14:22 +0000 |
| Subject: | note 82013 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137461@lists.php.net to get a copy of this message | ||
The following function ipToHex will take an IP (v4 or v6 formatted) and if it is valid, will return
a 32 byte hex string representing that address. Requires php >= 5.2 as it uses the filter_var
function.
<?php
function ipToHex($ipAddress) {
$hex = '';
if(strpos($ipAddress, ',') !== false) {
$splitIp = explode(',', $ipAddress);
$ipAddress = trim($splitIp[0]);
}
$isIpV6 = false;
$isIpV4 = false;
if(filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
$isIpV6 = true;
}
else if(filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
$isIpV4 = true;
}
if(!$isIpV4 && !$isIpV6) {
return false;
}
// IPv4 format
if($isIpV4) {
$parts = explode('.', $ipAddress);
for($i = 0; $i < 4; $i++) {
$parts[$i] = str_pad(dechex($parts[$i]), 2, '0', STR_PAD_LEFT);
}
$ipAddress = '::'.$parts[0].$parts[1].':'.$parts[2].$parts[3];
$hex = join('', $parts);
}
// IPv6 format
else {
$parts = explode(':', $ipAddress);
// If this is mixed IPv6/IPv4, convert end to IPv6 value
if(filter_var($parts[count($parts) - 1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
$partsV4 = explode('.', $parts[count($parts) - 1]);
for($i = 0; $i < 4; $i++) {
$partsV4[$i] = str_pad(dechex($partsV4[$i]), 2, '0', STR_PAD_LEFT);
}
$parts[count($parts) - 1] = $partsV4[0].$partsV4[1];
$parts[] = $partsV4[2].$partsV4[3];
}
$numMissing = 8 - count($parts);
$expandedParts = array();
$expansionDone = false;
foreach($parts as $part) {
if(!$expansionDone && $part == '') {
for($i = 0; $i <= $numMissing; $i++) {
$expandedParts[] = '0000';
}
$expansionDone = true;
}
else {
$expandedParts[] = $part;
}
}
foreach($expandedParts as &$part) {
$part = str_pad($part, 4, '0', STR_PAD_LEFT);
}
$ipAddress = join(':', $expandedParts);
$hex = join('', $expandedParts);
}
// Validate the final IP
if(!filter_var($ipAddress, FILTER_VALIDATE_IP)) {
return false;
}
return strtolower(str_pad($hex, 32, '0', STR_PAD_LEFT));
}
$ips = array(
'::192.168.0.2',
'0:0:0:0:0:0:192.168.0.2',
'192.168.0.2',
'::C0A8:2',
'0:0:0:0:0:0:C0A8:2'
);
$finals = array();
foreach($ips as $ip) {
$finals[] = ipToHex($ip);
}
var_dump($finals);
?>
----
Server IP: 209.41.74.194
Probable Submitter: 158.118.51.2
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/82013
Del: integrated -- https://master.php.net/note/delete/82013/integrated
Del: useless -- https://master.php.net/note/delete/82013/useless
Del: bad code -- https://master.php.net/note/delete/82013/bad+code
Del: spam -- https://master.php.net/note/delete/82013/spam
Del: non-english -- https://master.php.net/note/delete/82013/non-english
Del: in docs -- https://master.php.net/note/delete/82013/in+docs
Del: other reasons-- https://master.php.net/note/delete/82013
Reject -- https://master.php.net/note/reject/82013
Search -- https://master.php.net/manage/user-notes.php