Shafiqul Islam
Shafiqul Islam

Reputation: 5690

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

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

$ip = $_SERVER["REMOTE_ADDR"];

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

Upvotes: 7

Views: 27756

Answers (4)

drushadrusha
drushadrusha

Reputation: 11

Only for sake of experiment you could do something like this:

<?php

$ipv4 = $_GET['ipv4'] ?? "";
$ipv6 = $_GET['ipv6'] ?? "";

$ip = $_SERVER['REMOTE_ADDR'];
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    $ipv4 = $ip;
} elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    $ipv6 = $ip;
}

echo "Your IPv4 is: $ipv4 <br>";
echo "Your IPv6 is: $ipv6 <br>";

// Redirect user to this page with the other IP version if one is missing

if($ipv4 == ""){
    header("Location: http://127.0.0.1/ip.php?ipv6=$ipv6");
}

if($ipv6 == ""){
    header("Location: http://[::1]/ip.php?ipv4=$ipv4");
}

?>

But this code hardy has any practical application and you should use it with caution, as it does not provide validation of IP addresses, does not handle redirection loops, and does not account for cases where the user does not have access via IPv6 or IPv4(you could use javascript to detect that on client side).

You will also need to configure your web server to use this.

Upvotes: 1

Majika
Majika

Reputation: 13

This is just a thought but maybe the trick is how said client queries the server.

Its what I use to gain my Public WAN_IPv4 & Public WAN_IPv6 with a simple Cron job to fire every xx mins and update one of my DNS records

Why not go up a layer and query the hostname NS A and AAAA records itself?

$ dig dom.domain -A | grep "ip ..." [with some some regex to extract specific pattern.]

Or a combination of approaches, maybe even employ curl to grab the -ipv4 address and then use a direct call to "ping" the myip6.php script for IPv6 return.?

Or keep it simple via dns resolving on interface with cURL

$ curl --help all
...
--dns-interface <interface> Interface to use for DNS requests
--dns-ipv4-addr <address> IPv4 address to use for DNS requests
--dns-ipv6-addr <address> IPv6 address to use for DNS requests
--dns-servers <addresses> DNS server addrs to use
...
--interface <name>   Use network INTERFACE (or address)
-4, --ipv4               Resolve names to IPv`enter code here`4 addresses
-6, --ipv6               Resolve names to IPv6 addresses
...

if using curl you can specify option -4 and -6 to indicate which IP stack to return its result

$ curl -4 dom.domain
OUTPUT:
> 255.255.255.255

The same can be done for IPv6 by using -6

$ curl -6 dom.domin 
OUTPUT:
> 20a1:efef:ffff::0

Above can then of course can be piped out to either console or stored inside a _VAR for later use.

I mean this is not a direct method to answer your specific question BUT it does give you options when managing dual-ipstack machines and its what I do.

There is also a very good example on how you could use curl for this exact job [name-resolve-tricks-with-c-ares][1] [1]: https://everything.curl.dev/usingcurl/connections/name#name-resolve-tricks-with-c-ares

Upvotes: 0

deceze
deceze

Reputation: 522510

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.

Upvotes: 21

Quentin
Quentin

Reputation: 944320

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.

Upvotes: 13

Related Questions