Jump to content

Recommended Posts

Hi,

  I am using gethostbyname() function in my php script for getting the IP address of host. Its working well with IPv4. but if the host is having IPv6 if is not working. is there any alternative method in PHP to return IPv6 address by taking host name. please suggest on this.

  thanks in advance.

 

-sun

Link to comment
https://forums.phpfreaks.com/topic/157824-gethostbyname-for-ipv6/
Share on other sites

I found this in the PHP manual, I haven't used or tried it...

 

    function gethostbyname6($host, $try_a = false) {
        // get AAAA record for $host
        // if $try_a is true, if AAAA fails, it tries for A
        // the first match found is returned
        // otherwise returns false

        $dns = gethostbynamel6($host, $try_a);
        if ($dns == false) { return false; }
        else { return $dns[0]; }
    }

    function gethostbynamel6($host, $try_a = false) {
        // get AAAA records for $host,
        // if $try_a is true, if AAAA fails, it tries for A
        // results are returned in an array of ips found matching type
        // otherwise returns false

        $dns6 = dns_get_record($host, DNS_AAAA);
        if ($try_a == true) {
            $dns4 = dns_get_record($host, DNS_A);
            $dns = array_merge($dns4, $dns6);
        }
        else { $dns = $dns6; }
        $ip6 = array();
        $ip4 = array();
        foreach ($dns as $record) {
            if ($record["type"] == "A") {
                $ip4[] = $record["ip"];
            }
            if ($record["type"] == "AAAA") {
                $ip6[] = $record["ipv6"];
            }
        }
        if (count($ip6) < 1) {
            if ($try_a == true) {
                if (count($ip4) < 1) {
                    return false;
                }
                else {
                    return $ip4;
                }
            }
            else {
                return false;
            }
        }
        else {
            return $ip6;
        }
    }

Thanks for your reply. But dns_get_record() function fetches corresponding IP/IPv6 for the hostname from DNS.  But we dont have IPv6 DNS.

 

Thing is i've configured my linux box for IPv6 with tunneling mechanism and added its IPv6 entry in /etc/hostfile.

 

I want to know is there any function in PHP that resolves hostname from /etc/hostfile and return corresponding IP/IPv6? or else share with me if there is any other way for the same purpose.

 

 

thanks once again.

-SUN

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.