Importing IP2Proxy data into Redis and querying with PHP

The aim of this guide is to demonstrate how to import IP2Proxy data (PX11) 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 CSV file.
Download commercial version at


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 Redis Library:

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 and paste the following code into it:



# 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 ", "ZADD", db; printf "%s \"%s|", $1, $1; for(i=2; i<=NF; i++) { if(i>2) { 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:

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:

$ip = '';

function queryIP2Proxy($myip) {
    $redis = new Redis();
    $redis->connect(''); // port 6379 by default
    if ($redis->auth('your_redis_password')) {
        $ipnum = sprintf("%u", ip2long($myip));

        $result = $redis->zRevRangeByScore('PX11', $ipnum, 0, array('limit' => array(0, 1)));

        if (!empty($result)) {
            $result = $result[0];
            $arr = explode("|", $result);
            if ($arr[1] >= $ipnum) {
                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";


Was this article helpful?

Related Articles