amuck-landowner

IPv6 formatting help needed

KuJoe

Well-Known Member
Verified Provider
A client pointed out today that he couldn't set his rDNS record and it turns out it's because of the IPv6 formatting PHP does. For this thread here's the example IPv6 address I'm using: 1111:0001:0000:0003:0004:0005:0006:0007

Part A of my question is are both of these formats considered valid?

  • 1111:1:0:3:4:5:6:7
  • 1111:1::3:4:5:6:7
If they are, part B of my question is what is the best method to get PHP to check both formats? Here's my current code but it only returns 1111:1:0:3:4:5:6:7 as the valid format:

<?php
$ip = inet_ntop(inet_pton('1111:0001:0000:0003:0004:0005:0006:0007'));
echo $ip;
?>I'm surprised this is the first time this problem has cropped up but any help is appreciated. Thanks!
 
Last edited by a moderator:

AuroraZero

Active Member
Would an if else statement work here?

Like
 

<?php

if

$ip = inet_ntop(inet_pton('1111:0001:0000:0003:0004:0005:0006:0007'));

else

$ip = inet_ntop(inet_pton('1111:0001:0003:0004:0005:0006:0007'));
Code:
echo $ip;
Code:
fi

?>
maybe?  

Or I could be way off.
 
Last edited by a moderator:

KuJoe

Well-Known Member
Verified Provider
Last edited by a moderator:

KuJoe

Well-Known Member
Verified Provider
If you figured out a solution, great, but this seemed to do exactly what you asked for, unless I have miunderstood your need: http://www.w3schools.com/php/filter_validate_ip.asp
Either way, good luck :)

I probably didn't explain myself well enough in my first post. I've been rather sleep deprived the past few weeks while I'm in transition of moving (today was the first day I've actually been able to get on vpsBoard and post while I crash at my parent's house).

What I need to do is get the IP address from the client and compare it to AAAA records for a domain. I needed to find a way to allow clients to use 1111:1:0:3:4:5:6:7 or 1111:1::3:4:5:6:7 and have my script check for both.
 

telephone

New Member
What I need to do is get the IP address from the client and compare it to AAAA records for a domain. I needed to find a way to allow clients to use 1111:1:0:3:4:5:6:7 or 1111:1::3:4:5:6:7 and have my script check for both.
Run your IPv6 compact code (first post) on the clients IP:

Code:
<?php

// ##########  Function used for both options  ##########

function isValidIPv6($address)
{
    return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}

// ##########  Option #1  ##########

function getCompact($address)
{
    return inet_ntop(inet_pton($address));
}

// ---------------------------

/**
 * It doesn't matter whether a client/user uses either '1111:1:0:3:4:5:6:7' or '1111:1::3:4:5:6:7'
 * as they'll equal the same after using the compact function.
 */

$fullIP   = '1111:0001:0000:0003:0004:0005:0006:0007';  // IP from your system
$clientIP = '1111:1:0:3:4:5:6:7';                        // User IP - Variation #1

// validate user IP
if (isValidIPv6($clientIP)) {
    $compactFullIP   = getCompact($fullIP);   // = 1111:1:0:3:4:5:6:7
    $compactClientIP = getCompact($clientIP); // = 1111:1:0:3:4:5:6:7

    // compare addresses
    if ($compactFullIP === $compactClientIP) {
        echo 'IPs match!';
    }
    else {
        echo 'IPs do not match!!!';
    }
}
else {
    echo 'IP is not a valid IPv6 address';
}


// ##########  Option #2  ##########

function getFull($address)
{
    return implode(':', array_map(function($b) {
        return sprintf('%04x', $b);
    }, unpack('n*', inet_pton($address))));
}

// ---------------------------

/**
 * You can even do the reverse, and expand the users IP to the full format. Again, it doesn't
 * matter which format the client/user uses.
 */

$fullIP   = '1111:0001:0000:0003:0004:0005:0006:0007';  // IP from your system
$clientIP = '1111:1::3:4:5:6:7';                        // User IP - Variation #2

// validate user IP
if (isValidIPv6($clientIP)) {
    $compactFullIP   = getFull($fullIP);   // = 1111:0001:0000:0003:0004:0005:0006:0007
    $compactClientIP = getFull($clientIP); // = 1111:0001:0000:0003:0004:0005:0006:0007

    // compare addresses
    if ($compactFullIP === $compactClientIP) {
        echo 'IPs match!';
    }
    else {
        echo 'IPs do not match!!!';
    }
}
else {
    echo 'IP is not a valid IPv6 address';
}
 
Last edited by a moderator:

KuJoe

Well-Known Member
Verified Provider
@telephone I don't know why that never occurred to me. Thanks! :)
 
Top
amuck-landowner