Importing IP2Location data into Redis and querying with PHP (IPv6)

The aim of this guide is to demonstrate how to import IP2Location data (DB26 IPv6) in CSV form into Redis and then query the data in a PHP web page.

First of all, you will need to download the IP2Location DB26 IPv6 CSV file.
Download commercial version at https://ip2location.com/download?code=DB26IPV6

Extract out the IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.CSV file from the downloaded zipped file.

Important Note

We will not cover installation of Redis or PHP in this guide. We will assume you have already setup Redis on the localhost and are using PHP via Apache (also on the localhost). For this example, we are using a Debian Linux.

More info can be found at the following URLs if you need assistance with installations:
PHP: http://php.net/manual/en/install.unix.debian.php
Redis: http://redis.io/download
PHP Redis Library: http://pecl.php.net/package/redis

We will be using a Bash script to do mass import of CSV data into Redis and then querying the data via PHP using the PHP Redis library.

Importing the CSV data into Redis

As mentioned earlier, we will be using a Bash script to perform the import of the CSV data into Redis as we need to format our CSV data into an easier to store form.

Create a new file called importredis.sh and paste the following code into it:

#!/bin/bash

DB="DB26IPV6"
FILENAME="IPV6-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.CSV"

# delete old DB first
echo "DEL "$DB | redis-cli --verbose

# transform and import data
cat $FILENAME | awk -vdb="$DB" 'BEGIN { FS="\",\""; } { $NF = substr($NF, 1, length($NF) - 1); printf "%s %s 0 \"", "ZADD", db; padlen = 39 - length($2); for(i=0; i<padlen; i++) { printf "%s", "0"; } printf "%s|", $2; for(i=3; i<=NF; i++) { if(i>3) { printf "%s", "|"; } printf "%s", $i; if(i==NF) { print ""; } } }' | redis-cli --pipe --pipe-timeout 0 --verbose

Run the Bash script by calling the below command in command prompt:
bash importredis.sh

Querying the IP2Location data from a PHP web page

Now, create a PHP file called test.php in your website.

Paste the following PHP code into it and then run it in the browser:

<?php
$ip = '2001:1388:b4b:342e:ad92:a9e7:bc5b:9340'; // test IPv6
// $ip = '::ffff:8.8.8.8'; // test IPv4 (must be in IPv4-Mapped IPv6 format)

function ip62long($ipv6) {
    return (string) gmp_import(inet_pton($ipv6));
}

function queryIP2Location($myip) {
    $redis = new Redis();
    $redis->connect('127.0.0.1'); // port 6379 by default
    if ($redis->auth('your_redis_password')) {
        $ipnum = ip62long($myip);
        $ipnum = str_repeat('0', 39 - strlen($ipnum)) . $ipnum;

        $result = $redis->zRangeByLex('DB26IPV6', '['.$ipnum, '+', 0, 1);
        $result = $result[0];
        $arr = explode("|", $result);

        echo "country_code: " . $arr[1] . "<br>\n";
        echo "country_name: " . $arr[2] . "<br>\n";
        echo "region_name: " . $arr[3] . "<br>\n";
        echo "city_name: " . $arr[4] . "<br>\n";
        echo "latitude: " . $arr[5] . "<br>\n";
        echo "longitude: " . $arr[6] . "<br>\n";
        echo "zip_code: " . $arr[7] . "<br>\n";
        echo "time_zone: " . $arr[8] . "<br>\n";
        echo "isp: " . $arr[9] . "<br>\n";
        echo "domain: " . $arr[10] . "<br>\n";
        echo "net_speed: " . $arr[11] . "<br>\n";
        echo "idd_code: " . $arr[12] . "<br>\n";
        echo "area_code: " . $arr[13] . "<br>\n";
        echo "weather_station_code: " . $arr[14] . "<br>\n";
        echo "weather_station_name: " . $arr[15] . "<br>\n";
        echo "mcc: " . $arr[16] . "<br>\n";
        echo "mnc: " . $arr[17] . "<br>\n";
        echo "mobile_brand: " . $arr[18] . "<br>\n";
        echo "elevation: " . $arr[19] . "<br>\n";
        echo "usage_type: " . $arr[20] . "<br>\n";
        echo "address_type: " . $arr[21] . "<br>\n";
        echo "category: " . $arr[22] . "<br>\n";
        echo "district: " . $arr[23] . "<br>\n";
        echo "asn: " . $arr[24] . "<br>\n";
        echo "as: " . $arr[25] . "<br>\n";
    }
    else {
        echo "Incorrect password\n";
    }
    $redis->close();
}

queryIP2Location($ip);
?>

Was this article helpful?

Related Articles