Converting IPv6 to IPv4 address in PHP



PHP Snippet 1:

<? // Grab the IP address of the user
$ipaddress = getenv("REMOTE_ADDR") ; echo 'IP Address: '.$ipaddress;

// Check if we need to try and get the IPv4 address
if(filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
?>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>

      <script>

    /* Add "https://api.ipify.org?format=json" statement
               this will communicate with the ipify servers in
               order to retrieve the IP address $.getJSON will
               load JSON-encoded data from the server using a
               GET HTTP request */

    $.getJSON("https://api.ipify.org?format=json", function(data) {

        // Setting text of element P with id gfg
        $("#gfg").html(data.ip);
    })
    </script>

       <p id="gfg"></p>
<?
}
else {
}

?>