0

On my website, which runs with apache2 on a debian server with an aaa-record, I check with php whether the user uses IPv6 or not. For this I get the IP address of the user with the following code shippet:

if (isset($_SERVER['HTTP_CLIENT_IP'])){
   $ip = $_SERVER['HTTP_CLIENT_IP'];
}else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
   $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else if(isset($_SERVER['HTTP_X_FORWARDED'])){
   $ip = $_SERVER['HTTP_X_FORWARDED'];
}else if(isset($_SERVER['HTTP_FORWARDED_FOR'])){
   $ip = $_SERVER['HTTP_FORWARDED_FOR'];
}else if(isset($_SERVER['HTTP_FORWARDED'])){
   $ip = $_SERVER['HTTP_FORWARDED'];
}else if(isset($_SERVER['REMOTE_ADDR'])){
   $ip = $_SERVER['REMOTE_ADDR'];
}else{
   echo "ERROR";
}

This works all totally fine. But sometimes I get an IPv4 addresse even if I have an Ipv6 connection. And this can't be an error of my internet connection because when if visit an ip check website (like this one) it shows me an ipv6 address. I can also ping my server with ping -6 server_ip. So there must be a problem with my aaa-record or with apache2 or maybe with php. I hope someone can help me.

1
  • Why do you think it is a problem that some requests used IPv4? Oct 22, 2020 at 19:04

1 Answer 1

1

There is nothing wrong with that. Since your website resolves to both IPv4 and IPv6 addresses, the browser (or whatever HTTP client) at the other end may pick an address in random order, or prefer IPv6 over IPv4 (or the other way round). It may very well connect over IPv4 sometimes, and IPv6 at other times. The behavior has to be browser-specific and implementation will vary a lot from one software to another. Maybe this post can help too.

2
  • Thank you for the answer. But how does sites like whatismyipaddress.com get the ipv4 and ipv6 address? Oct 24, 2020 at 12:26
  • Simply by embedding resources in the page like images, scripts etc that resolve to IPv4 or IPv6 addresses, thus forcing your browser to make HTTP connections over both protocols.
    – Kate
    Oct 24, 2020 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .