7

I have a question with variable : $_SERVER['REMOTE_ADDR']. I wish he recovers ONLY IP addresses in IPv4 format . It extracted me a few times in IPv6 format .

How to do?

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Aymeric98
  • 137
  • 2
  • 2
  • 9

2 Answers2

18

$_SERVER['REMOTE_ADDR'] always contains the address of the visitor. If it contains an IPv6 address then the visitor used IPv6 and there is no IPv4 address. And vice versa of course. These days you have to be able to deal with both.

Some visitors will have only IPv4, some will have only IPv6 and some will have both. The browser decides what is available and what it will use, and that's all you'll see. Note that a browser that has both might even switch between IPv4 and IPv6 between requests if it deems that necessary for good connectivity.

Sander Steffann
  • 9,509
  • 35
  • 40
1

It's not possible if the user connects to your site via IPv6.

If you really want to know the ipv4 of the user, you could do it in 2 ways:

a) remove the AAAA records of your domain, so it will only work via ipv4 and all the users will be forced to connect via ipv4. But this is a bad solution because ipv6-only users that don't have ipv4 connectivity will not be able to reach your site.

b) you could create an ajax endpoint to another domain that only resolves to ipv4, and get their IP from there.

the_nuts
  • 5,634
  • 1
  • 36
  • 68