How can I get both IPv4 and IPv6 address using PHP code?

13,652

Solution 1

You can't.

Only the IP address the request came from is available.

There's no reliable way to identify other IP addresses (my laptop currently has 12 IP addresses) that route to the same computer.

Solution 2

A client will send a request to your server using only one protocol. It doesn't send the request using both IPv4 and IPv6 at the same time, and there's no way to interleave both protocols, and IPv4 addresses also don't translate into equivalent IPv6 addresses. If the client sent the request using IPv4, then you'll get the IPv4 address. If they sent the request using IPv6, you'll get the IPv6 address. Period. End of story.

If you require an IPv4 address, then you have to disable IPv6 support on your server/DNS entry, so all clients are forced to use IPv4 as the only available protocol. But that would also be a terrible step backwards in this day and age.

Share:
13,652
Shafiqul Islam
Author by

Shafiqul Islam

I want to explore myself with latest technology and also want to develop my Career with well-established and fast growing IT Industry. I am working Digital gold and Crypto exchange. This is Crypto market exchange system. From this site user can deposit Bitcoin, Ether, USD etc. and also convert one to another. User can withdraw it into others account. User can convert gold to token and many others option. There are many features in this site. Technology : PHP, Laravel, Wordpress, Ruby on Rails, Redis, Sidekiq, Customerio, Aws::S3, React, Flutter, Dart, Firebase, Bootsnap, Jenkins, Spring Boot, MYSQL, Postgres, OAuth 2.0, Rabbitmq, Capistrano, Devops

Updated on June 04, 2022

Comments

  • Shafiqul Islam
    Shafiqul Islam almost 2 years

    I have a script which sends a request to another server but problem is that IPv6 does not supported so if I send IPv6 then give error so i need this one of two:

    1. Get IPv4 address all time or
    2. Get both IPv4 and IPv6 addresses

    I use this code to get the IP address

    function getRealIP()
        {
            if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $ip = $_SERVER["HTTP_CLIENT_IP"];
            } elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } elseif (isset($_SERVER["HTTP_X_FORWARDED"])) {
                $ip = $_SERVER["HTTP_X_FORWARDED"];
            } elseif (isset($_SERVER["HTTP_FORWARDED_FOR"])) {
                $ip = $_SERVER["HTTP_FORWARDED_FOR"];
            } elseif (isset($_SERVER["HTTP_FORWARDED"])) {
                $ip = $_SERVER["HTTP_FORWARDED"];
            } else {
                $ip = $_SERVER["REMOTE_ADDR"];
            }
    
            // Strip any secondary IP etc from the IP address
            if (strpos($ip, ',') > 0) {
                $ip = substr($ip, 0, strpos($ip, ','));
            }
            return $ip;
        }
    

    But this function only returns one IPv address. How can I get all time IPv4 or get both addresses?

  • Jeffrey Kastner
    Jeffrey Kastner over 5 years
    IF you can't, then how IS it being done here? whatismyipaddress.com
  • Sampsa Suoninen
    Sampsa Suoninen about 5 years
    This is most likely done with a redirect (using Javascript?) or a request to IPv6 or a IPv4 only address respectively and using that to return a response with the missing address. For example: Connect through IPv4, that IP is now found. Use a Javascript to request to ipv6test.service.tld to get missing address.
  • Elijah Mock
    Elijah Mock about 4 years
    So then, if you are trying to test if someone has a certain IP, you would need to be prepared for both IP addresses to be incoming? (I know spoofing exists still)
  • Mark Lee
    Mark Lee almost 3 years
    These accepted answers need updating because, as pointed out by @Jeffrey Kastner back in 2018 and still happens today (July 2021), some services display a visitor's IPV 4 and IPV6 ip addresses.
  • Mark Lee
    Mark Lee almost 3 years
    What I discovered is that my router operates in dual mode so that whoever checks can identify both. I disabled IPV6 for my purposes since it seems difficult to connect remotely using IPV6.
  • Gromski
    Gromski almost 3 years
    @Mark One request can only be sent using one IP address and protocol. If your router is supporting both, then a website can be rigged to include some IPv6-only and IPv4-only resources, and through cookies or other ids you can consolidate those separate requests with their separate IPs to one user. It’s not impossible to find multiple IPs per user. That doesn’t usually help you much though.
  • Mark Lee
    Mark Lee almost 3 years
    @decze whatismyipaddress.com goes to great lengths to make both available it would seem. I rigged my own dynamic ip on a website for some audio streaming but it now arbitrarily skips between the two versions.
  • Gromski
    Gromski almost 3 years
    @Mark Yes, they appear to have the explicit subdomain ds4.whatismyipaddress.com, which is only served over IPv4, and the page makes an explicit request to that with a unique token, so it can be linked back to the original IPv6 request (or vice versa).
  • Andrew Rout
    Andrew Rout about 2 years
    Most likely by embedding resources in the page like images, scripts etc that resolve to IPv4 or IPv6 addresses, as a result forcing your browser to make HTTP connections over both protocols.