Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How show IPv4 only with REMOTE_ADDR in PHP?

Tags:

php

ip

ipv4

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?

like image 481
Aymeric98 Avatar asked Jul 05 '15 17:07

Aymeric98


1 Answers

$_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.

like image 188
Sander Steffann Avatar answered Sep 22 '22 15:09

Sander Steffann