0

I have IP database for a certain country. I have paid version of Ip2Lcoation and i got all the IPv6 ranges and IPv4. In my web app I am converting all the IPs to long using ip2long function.

It works perfectly with IPv4 but with IPv6 it fails? i searched and found few functions which didn't work. My Code is as follows

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    
    
    $long = ip2long($ip);// fails at IPv6
    //mysql query to check the ip ranges
    $cri= new CDbCriteria();
    $cri->condition="$long>=ip_range_start AND $long<=ip_range_end";
    if (UsaIps::model()->exists($cri))
    {
       return true;
    }
    else
    {
        return false;
    }

How can i achieve this ??

Any guide any help??

kool_Coder
  • 149
  • 1
  • 12
  • You can't convert an IPv6 address to a `long` value. IPv6 addresses uses more space than the `long` type can provide. Why do you want to do that? What is `CDbCriteria` and `UsaIps::model` and how is it related to "Ip2Location"? – Progman Jun 19 '21 at 13:21
  • i have a database from Ip2location in my web app i want some functions exclusively for USA. CdbCriteria and UsaIps::model is PHP YII Framework syntax. Ignore that – kool_Coder Jun 19 '21 at 13:24
  • 2
    Check https://www.ip2location.com/faqs#convert-ipv6-to-ip-number on how to use IPv6 addresses. If that doesn't work, [edit] your question to include a description how the content of the Ip2Location database looks like. You might also want to ask the authors of Ip2Location on how to use their product with IPv6 addresses. – Progman Jun 19 '21 at 13:31
  • An ipv4 address is a 32-bit, unsigned integer (32 bit in size). An IPv6 address is a 128-bit, unsigned integer (four times the size of an IPv4 address), and there is no integer type that large. You could use an array of four 32-bit, unsigned integers to represent an IPv6 address, or it may be more convenient for IPv6 networks (should almost all be `/64`) to use an array of two 64-bit, unsigned integers. – Ron Maupin Jun 19 '21 at 15:33

0 Answers0