How to lookup for proxy record using PHP and MySQL

This tutorial shows you on how to lookup for a proxy record from the MySQL database using PHP programming language. This tutorial uses IP2Proxy PX11 database as the example, but the logic can be used for other IP2Proxy PX database query, namely PX1PX2PX3, PX4, PX5, PX6, PX7, PX8, PX9, PX10.

Set up the IP2Proxy PX11 database

Firstly, you need to set up the proxy database for the query. You can download a free PX11 LITE version at https://lite.ip2location.com/ip2proxy-lite to give it a try. However, please take note that the LITE version only contains the PUB (public proxies) records only. If you are looking for the complete proxy database, including Public Proxies, Web Proxies, VPN, TOR, data centers, search engine robots and residential, you may visit https://www.ip2location.com/database/px11-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential-provider for details.

Run the below script to create and import the proxy database.

Create the table

CREATE DATABASE ip2proxy;
USE ip2proxy;
CREATE TABLE `ip2proxy_px11`(
    `ip_from` INT(10) UNSIGNED,
    `ip_to` INT(10) UNSIGNED,
    `proxy_type` VARCHAR(3),
    `country_code` CHAR(2),
    `country_name` VARCHAR(64),
    `region_name` VARCHAR(128),
    `city_name` VARCHAR(128),
    `isp` VARCHAR(256),
    `domain` VARCHAR(128),
    `usage_type` VARCHAR(11),
    `asn` VARCHAR(6),
    `as` VARCHAR(256),
    `last_seen` INT(10),
    `threat` VARCHAR(128),
    `provider` VARCHAR(256),
    PRIMARY KEY (`ip_from`, `ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Import the data into table

LOAD DATA LOCAL
    INFILE 'IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.CSV'
INTO TABLE
    `ip2proxy_px11`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Writing the code using PHP

Now, we are going to write the code using the PHP to lookup for the proxy record. The below codes will query the MySQL for the proxy record, and it will print out the record on screen if exists.

<?php
    //Open connection to mysql server
    $mysqli = new mysqli('localhost', 'root', '', 'ip2location');
 
    //Check connection
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
        exit();
    }
 
    //Check if the IP address is a proxy address
    $ip_address = '4.0.0.37';
    $result = $mysqli->query('SELECT * FROM ip2proxy_px11 WHERE inet_aton("' . $ip_address . '") between ip_from AND ip_to LIMIT 1');
 
    if ($result->num_rows > 0){
        //proxy
        $row = $result->fetch_assoc();
        echo 'IP Address: ' . $ip_address . '<br>';
        echo 'Proxy Type: ' . $row['proxy_type'] . '<br>';
        echo 'Country Code: ' . $row['country_code'] . '<br>';
        echo 'Country Name: ' . $row['country_name'] . '<br>';
        echo 'Region Name: ' . $row['region_name'] . '<br>';
        echo 'City Name: ' . $row['city_name'] . '<br>';
        echo 'ISP: ' . $row['isp'] . '<br>';
        echo 'Domain: ' . $row['domain'] . '<br>';
        echo 'Usage Type: ' . $row['usage_type'] . '<br>';
        echo 'ASN: ' . $row['asn'] . '<br>';
        echo 'AS: ' . $row['as'] . '<br>';
        echo 'Last Seen: ' . $row['last_seen'] . '<br>';
        echo 'Threat: ' . $row['threat'] . '<br>';
        echo 'Provider: ' . $row['provider'] . '<br>';
    }
    else{
        //non-proxy
        echo $ip_address . ' is not a proxy<br>';
    }
?>

 

Was this article helpful?

Related Articles