Jump to content

IP2Location Database IPv6 conversion to IP Number Decimal


Go to solution Solved by Barand,

Recommended Posts

I just downloaded and installed IP2Location database and everything is smooth for IPv4 addresses. (If you want to get location from peoples IP the free version is here: http://lite.ip2location.com/database-ip-country-region-city-latitude-longitude-zipcode-timezone) You convert the IPv4 address to an IP number and then do a database query. See here:

$ip_split = split ("\.", "$ip");
$ip_num = $ip_split[3] + ($ip_split[2] * 256) + ($ip_split[1] * 256 * 256) + ($ip_split[0] * 256 * 256 * 256);

$result = mysql_query("SELECT * FROM ip2location_db11 WHERE '$ip_num' BETWEEN ip_from AND ip_to");

However I now need to do this for an IPv6 address but there is no documentation or anything I can find Googleing about how to do this for IPv6. Obviously the same method will not work as it has letters in the address as well as more numbers. There is obviously some difference to it. Anyone know what to do?

 

Thanks!

mysql> SELECT INET_ATON('199.119.180.52') as ip_num;
+------------+
| ip_num     |
+------------+
| 3346510900 |
+------------+

Therefore you can

$ip_add = '199.119.180.52';

$sql = "SELECT * 
        FROM ip2location_db11 
        WHERE INET_ATON('$ip_add') BETWEEN ip_from AND ip_to";

I tried this and it returned empty.

$ip_test = '2607:f0d0:1002:51::4';
$result = mysql_query("SELECT * FROM ip2location_db11_ipv6 WHERE INET_ATON('$ip_test') BETWEEN ip_from AND ip_to");
$location_record = mysql_fetch_array($result);

Found the answer but new issue now. Here is the code:

$ipv6 = '2404:6800:4001:805::1006';
$int = inet_pton($ipv6);
$bits = 15;

$ipv6long = 0;

while($bits >= 0){
	$bin = sprintf("%08b", (ord($int[$bits])));

	if($ipv6long){
		$ipv6long = $bin . $ipv6long;
	}
	else{
		$ipv6long = $bin;
	}
	$bits--;
}

$ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);

Now the only problem is I do not have math functions installed. I would prefer not to install them. Is there an alternate for the two gmp functions?

  • Solution

 

I tried this and it returned empty.

$ip_test = '2607:f0d0:1002:51::4';
$result = mysql_query("SELECT * FROM ip2location_db11_ipv6 WHERE INET_ATON('$ip_test') BETWEEN ip_from AND ip_to");
$location_record = mysql_fetch_array($result);

 

:rtfm:  If you are working with ip6 then you need INET6_ATON() . This function requires MySQL 5.6+

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.