What defines the address is the connection
The variable $_SERVER["REMOTE_ADDR"];
shows both Ipv4 and Ipv6. It only depends on how the server network is configured, and how the client accessed their page.
If the machine is serving an address via Ipv4 you will get something like:
208.67.222.222
if you are serving for Ipv6, you will already have something on that line:
2001:0db8:85a3:08d3:1319:8a2e:0370:7344
Edit in response to a comment: if you already have a server that serves Ipv6, just force the Ipv6 request to your server to see the REMOTE_ADDR
working in practice. For example, putting Ipv6 in the href
of some link, or in the URL of a iframe
or requisition ajax if you prefer. Nothing prevents you from testing both Ipv4 and Ipv6 on the same page, but for this you need to make both requests separately.
To standardize the storage
PHP has the function inet_pton()
, which understands both forms, and converts the address to a binary compact version.
Just to illustrate, I made a small function that converts any valid IP into Ipv6 long:
<?php
function normalizeIP( $ip ) {
$ip = inet_pton( $ip );
if( strlen( $ip ) < 5 ) {
$ip = chr( 255 ).chr( 255 ).str_pad( $ip, 4, chr( 0 ), STR_PAD_LEFT );
}
$ip = str_split( str_pad( $ip, 16, chr( 0 ), STR_PAD_LEFT ) );
$out = '';
for( $i = 0; $i < 16; ) {
if( $i && $i % 2 == 0 ) $out .= ':';
$out .= str_pad( dechex( ord( $ip[$i++] ) ), 2, '0', STR_PAD_LEFT );
}
return $out;
}
?>
See on IDEONE.
Of course in practice you probably won’t need any of this, just store the result of inet_pton()
in a field that accepts binary strings of variable size up to 16 characters.
In short
This depends only on the configuration of the server connection, and even if the machine meets both protocols, it may happen to the client A
be using Ipv4, and a customer B
by Ipv6. Both will have their respective IP stored in $_SERVER["REMOTE_ADDR"]
.
If you have both protocols enabled on the server, may be that Ipv4 is converted to an Ipv6 notation and you get these results (note the prefix ::ffff
indicating that it is an Ipv4 in Ipv6 format):
::ffff:192.000.002.124
::ffff:192.0.2.124
0000:0000:0000:0000:0000:ffff:c000:027c
::ffff:c000:027c
::ffff:c000:27c
All addresses above are equivalent to Ipv4 192.0.2.124
.
What I want to do is what appears on these links my ipv4 (http://ip-lookup.net/? 85.246.154.130) and my ipv6 (http://ip-lookup.net/? 2002:55F6:9A82:0:0:0:0:0)
– Ricardo Jorge Pardal