-2

I'd like to show my visitors their IPv4 address and if they have a IPv6 and IPv4 address I would like it to only show the IPv4 address. Here is what I have currently:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
  $ip = $_SERVER['REMOTE_ADDR'];
}

echo $ip;

If a visitor has an IPv6 address the above will show that instead of their IPv4 address, how can I prevent this and only show their IPv4 address?

Conserta
  • 99
  • 1
  • 2
  • 12
  • Some clients simply don't have an IPv4 address. AFAIK, there's no way to say "oh this IPv6 address maps to this IPv4 address" – Mave May 29 '15 at 07:14
  • This is just for the visitors that do have both IPv4 and IPv6 such as all the AT&T customers in the US. I'm not worried about the visitors who only have an IPv6 address. – Conserta May 29 '15 at 07:21
  • Do a `print_r($_SERVER)`, see if that turns up your IPv4 somewhere. – Mave May 29 '15 at 07:24
  • Thanks, I can't check this right now I'm on a IPv4 only net connection. Anyone that has both and wants to check this that would be awesome. – Conserta May 29 '15 at 07:27
  • @Mave It's true that there are users who don't have an IPv4 address. But the vast majority of those will have access to NAT64, and websites which they visit will be able to see the IPv4 address of the NAT64. Really no different from NAT44 where websites also see the IPv4 address of the NAT rather than that of the client. – kasperd May 30 '15 at 04:09

3 Answers3

2

I think what you are requesting is impossible, IPV6 have a larger range of valid combination compared to IPV4. This in short equates that there will be IPV6 values that don't map to an IPV4

Vhortex
  • 381
  • 1
  • 7
  • ipchicken.com only shows my ipv4 address while the script above shows only my ipv6 address, so I know it's possible. – Conserta May 29 '15 at 07:18
  • It is normally the host server who dictates what you will get. When hosting that script on an IPV6 enabled host which prioritized IPV6 over IPV4, you will get an IPV6 which makes sense since more and more IPV6 values are now being served with no equivalent IPV4 value. I hosted your script on my domain and I get an IPV4 format, I have a friend test it and he was using pure IPV6, he gets a blank information. On my cloud hosting, he gets his IPV6 address and I got my IPV4 address – Vhortex May 29 '15 at 07:27
  • That makes perfect sense. So if I host the script from a server with only a IPv4 address and no IPv6 address, it will only show IPv4. Yes, very logical indeed. Thanks! – Conserta May 29 '15 at 07:32
2

Your question doesn't make any sense. You cannot assume that everybody has an IPv4 address these days.

IPv4 and IPv6 are separate protocols that operate side by side. A user can have either or both.

Sander Steffann
  • 9,509
  • 35
  • 40
0

You can only find one client IP address per request. If you want to display both address to the client, you need two requests. This can be done using javascript or an iframe.

To do it properly, you are going to need three domains.

  • A primary domain with dual stack
  • An IPv4-only domain.
  • An IPv6-only domain.

When the user access the primary domain their browser will decide whether to use IPv4 or IPv6. If an up to date browser is being used it will within a few milliseconds figure out which protocol is more efficient on this particular connection.

When you process the request you will find one of the two addresses. You can then include a link to a URL which will find an address of the other family. Be aware that this will fail if the user has access to only one of the two address families.

Be careful about proxies. If you trust X-Forwarded-For from an untrusted proxy, the user can trivially spoof any IP address.

Also notice that in case of a legitimate proxy, you will have no control over the address family used between client and proxy. It is entirely possible that they are using a proxy because the client machine only has an IP address of one of the two families, and in that case you are not going to find a client address in the other family, because it simply does not exist. You can still use the method described above to find both addresses of the proxy - assuming that the proxy actually allows the user to access both IPv4 and IPv6.

In more advanced configurations it is even possible that the user use a proxy for some requests and not for other requests. So you need to be prepared to handle a situation where only one of the two requests used a proxy.

In most cases the best approach is to simply ignore all that proxy information and only use the IP address you actually got the request from.

kasperd
  • 1,952
  • 1
  • 20
  • 31
  • I just iframed the above php script from an OpenVZ server that only has an IPv4 address on it, and it shows only an IPv4 address. Thank you for the insight though, very informative. – Conserta Jul 21 '15 at 01:08