1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to using php to get ipv6 address ?

Discussion in 'PHP' started by mmjanjust, Sep 23, 2010.

  1. #1
    hi all,
    how to get user's ipv6 address by php ?
     
    Last edited: Sep 23, 2010
    mmjanjust, Sep 23, 2010 IP
  2. canishk

    canishk Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try the following function, it will convert a IPv4 address to IPv6
    
    /**
     * Convert an IPv4 address to IPv6
     *
     * @param string IP Address in dot notation (192.168.1.100)
     * @return string IPv6 formatted address or false if invalid input
     */
    function IPv4To6($Ip) {
        static $Mask = '::ffff:'; // This tells IPv6 it has an IPv4 address
        $IPv6 = (strpos($Ip, '::') === 0);
        $IPv4 = (strpos($Ip, '.') > 0);
    
        if (!$IPv4 && !$IPv6) return false;
        if ($IPv6 && $IPv4) $Ip = substr($Ip, strrpos($Ip, ':')+1); // Strip IPv4 Compatibility notation
        elseif (!$IPv4) return $Ip; // Seems to be IPv6 already?
        $Ip = array_pad(explode('.', $Ip), 4, 0);
        if (count($Ip) > 4) return false;
        for ($i = 0; $i < 4; $i++) if ($Ip[$i] > 255) return false;
    
        $Part7 = base_convert(($Ip[0] * 256) + $Ip[1], 10, 16);
        $Part8 = base_convert(($Ip[2] * 256) + $Ip[3], 10, 16);
        return $Mask.$Part7.':'.$Part8;
    }
    
    PHP:
     
    canishk, Sep 23, 2010 IP
  3. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #3
    the same way as ipv4 :)
    using $_SERVER['REMOTE_ADDR']

    if that variable contains ipv4 address it means that client doesnt have ipv6, your server has missconfigured ipv6, your webserver doesnt have ipv6 support, your domain doesnt have aaaa record or if your site got both ipv4 and ipv6 address and everything is configured properly then most likely clients primary routing is via ipv4 (which is unusual but also might happen - might be forced by client or clients isp).
     
    tiamak, Sep 26, 2010 IP
  4. mmjanjust

    mmjanjust Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    i have test my server have support ipv6 using
    IPv6test -f /proc/net/if_inet6 && echo "Running kernel is IPv6 ready" to test my server ,
    actually , i want to get user ipv4 and ipv6 when user have visit my site ,
    is it possible ?
    anyone can give me more example ?
     
    mmjanjust, Sep 26, 2010 IP
  5. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #5
    but do u have ipv6 addresses assigned to your server? if not you should get /64 or /48 from some ipv6 tunnel broker (google: "ipv6 tunnel"; tunnelbroker.net is the easiest way).
    Each tunnel broker will provide you with additional software or instructions how to enable your newly assigned ipv6 addresses to your server.
    once u get ipv6 addresses up and running (test with "ping6 someipv6hostname" command), you need to assign it to your domain (add AAAA record for your domain, the same way u add A record for ipv4)
    last step is to bind your webserver to ipv6 address (most likely you will find example how to do it in your webserver config file, it is also possible that your webserver already binds on all available interfaces so u dont need to change anything).
     
    tiamak, Sep 27, 2010 IP