When accessing one of my sites on the www and displaying $_SERVER['REMOTE_ADDR'], sometimes it shows an ipv4 address, sometimes ipv6. Fair enough. I understand that the connection will be one or the other, not both. But - I want to get both addresses. Sites like whatsmyipaddress.com are able to show both. Does anyone know how they are accomplishing this? Many thanks.
I wrote one of those "what is your IP address" services, at https://myip.addr.space/ so I can tell you exactly how it is done.
First, any single connection is only going to go over IPv6 or IPv4, but not both. It's impossible to get both addresses with a single request.
So, on the first request, we show the IPv6 or IPv4 address it came in on.
Next, we do some AJAX queries with jQuery, to subdomains that specifically have only an IPv4 address or only an IPv6 address, so that they only work on IPv4 or IPv6 respectively, and thus are guaranteed to return an IPv4 or IPv6 address.
This is done in the DNS like so:
$ host myip.addr.space
myip.addr.space is an alias for www.addr.space.
www.addr.space has address 144.217.146.101
www.addr.space has IPv6 address 2607:5300:203:118:1:0:3:8b50
$ host ipv4.myip.addr.space
ipv4.myip.addr.space has address 144.217.146.101
$ host ipv6.myip.addr.space
ipv6.myip.addr.space has IPv6 address 2607:5300:203:118:1:0:3:8b50
The jQuery is very simple:
<script type="text/javascript">
<!--
$.get("https://ipv4.myip.addr.space/ajaxlookup")
.done(function(data) {
    $("div#ipv4-address").html(data);
})
.fail(function(data) {
    $("div#ipv4-address").html("<p>Couldn't determine an IPv4 address. You might not have IPv4 connectivity.</p>");
});
$.get("https://ipv6.myip.addr.space/ajaxlookup")
.done(function(data) {
    $("div#ipv6-address").html(data);
})
.fail(function(data) {
    $("div#ipv6-address").html("<p>Couldn't determine an IPv6 address. You might not have IPv6 connectivity.</p>");
});
</script>
This results in up to three IP address outputs:

What this doesn't do is to associate your IPv6 address with your IPv4 address. You'll need to use something like a cookie if you want to track a user as their IP address changes. There's nothing conceptually different here from how you would handle a user whose IPv4 address changed to a different IPv4 address, except perhaps that you have to throw out the practice of tying cookies to IP addresses if you had been doing that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With