How to make a page to know Ipv6?

Asked

Viewed 883 times

3

To know Ipv4 I can use the code on a page:

<?php
    echo $_SERVER["REMOTE_ADDR"];
?>

But I can not find anything similar to do the same with Ipv6, what I get a lot is already sites with converters.

Even if there is no code like the above for Ipv6, I would like to be able to make one that fixes the conversion automatically to Ipv6.

And the end result was for example 2002:55F6:9A82:0:0:0:0:0 as can be seen in link

  • 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)

1 answer

8

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.

  • I could not get it to work with this code however I put as a comment of the question a site with the desired result, thanks @Bacco

  • 1

    The code works perfectly, only you need to make the DNS Ipv6 request on your page, otherwise it won’t. Just put the server Ipv6 in the link href, that the $_SERVER['REMOTE_ADDR']; comes right.

  • OK @Bacco I need you to explain to me with an example that I’m not getting and I’m not getting to practice.

  • 2

    make a page with 2 iframes, in one of them you poe <iframe src="ipv4.do.server/teste1.php"> on the other <iframe src="ipv6.do.server/teste1.php"> with the same page. Each one will show a different IP of the client. If he does not have either, one of the frames will give error. If he’s got both, it’s gonna look different on the frames. MAYBE you have to create a note in your DNS, for example ipv6.example.with only AAAA, and ipv4.example.with only A, so the addresses will only meet in the respective IP. If your hosting meets by IP (you have own ip) then you can use the same ip number.

  • Well @Bacco not even with its practical explanation I got there is as they say old donkey does not learn languages, if you do not care much I opened an ftp account on the server that I liked to pass you but in your profile there is no email as I can do?

  • 1

    I have no way to prepare a demonstration for not having even Ipv6 here to test, I’m going to owe this one. If there’s a little time left later, I’ll see if I can edit the answer with a demo. I can’t promise, but I’ll try to do it at some point.

  • Okay thanks just the same.

  • @Rjpservidor Do you have your own IP on your hosting? I mean, can you get it through a page of your own and access it only via IP? something accessible by http://89.xx.xx.xx/teste.php ?

  • Yes with you no problem

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.