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

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

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

Extract out the IP2PROXY-IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.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="PX11IPV6"
FILENAME="IP2PROXY-IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.CSV"

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

# transform and import data
cat $FILENAME | awk -vdb="$DB" 'BEGIN { FS="\",\""; } { $1 = substr($1, 2); printf "%s %s 0 \"", "ZADD", db; padlen = 39 - length($2); for(i=0; i<padlen; i++) { printf "%s", "0"; } printf "%s|", $2; padlen = 39 - length($1); for(i=0; i<padlen; i++) { printf "%s", "0"; } printf "%s|", $1; 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 IP2Proxy 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 queryIP2Proxy($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('PX11IPV6', '['.$ipnum, '+', 0, 1);
        if (!empty($result)) {
            $result = $result[0];
            $arr = explode("|", $result);

            if (strcmp($arr[1], $ipnum) <= 0) {
                echo "is proxy\n";
                echo "proxy type: " . $arr[2] . "\n";
                echo "country code: " . $arr[3] . "\n";
                echo "country name: " . $arr[4] . "\n";
                echo "region name: " . $arr[5] . "\n";
                echo "city name: " . $arr[6] . "\n";
                echo "isp: " . $arr[7] . "\n";
                echo "domain: " . $arr[8] . "\n";
                echo "usage type: " . $arr[9] . "\n";
                echo "asn: " . $arr[10] . "\n";
                echo "as: " . $arr[11] . "\n";
                echo "last seen: " . $arr[12] . "\n";
                echo "threat: " . $arr[13] . "\n";
                echo "provider: " . $arr[14] . "\n";
            }
            else {
                echo "Not proxy\n";
            }
        }
        else {
            echo "Not proxy\n";
        }
    }
    else {
        echo "Incorrect password\n";
    }
    $redis->close();
}

queryIP2Proxy($ip);
?>

Was this article helpful?

Related Articles