Search code examples
phpip

PHP Echo ipv4 and Ipv6


How Do I make it so my script echo's both IPv4 And IPv6, but if 1 of them is not detected it will say ''Not Detected''.

<?php
echo 'IPv4 '.$_SERVER['REMOTE_ADDR'];  
?>

Solution

  • There's only one address, IPv4 or IPv6. You can detect using the filter extension

    $ip = $_SERVER['REMOTE_ADDR'];
    if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
           echo "IPv6 detected";
     }
    else {
          echo "IPv4";
    }