How can one determine both IPv4 and IPv6 addresses separately in PHP using $_SERVER['REMOTE_ADDR']?

To determine both IPv4 and IPv6 addresses separately in PHP using $_SERVER['REMOTE_ADDR'], you can check if the IP address is in IPv4 or IPv6 format using the filter_var() function with the FILTER_VALIDATE_IP flag. If the IP address is in IPv4 format, you can store it in a variable for later use. If it is in IPv6 format, you can store it in a separate variable.

$ipAddress = $_SERVER['REMOTE_ADDR'];

if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    $ipv4Address = $ipAddress;
} elseif (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    $ipv6Address = $ipAddress;
}

echo "IPv4 Address: " . $ipv4Address . "<br>";
echo "IPv6 Address: " . $ipv6Address;