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 PX8 database as the example, but the logic can be used for other IP2Proxy PX database query, namely PX1PX2PX3, PX4, PX5, PX6, PX7.

Set up the IP2Proxy PX8 database

Firstly, you need to setup the proxy database for the query. You can download a free PX8 LITE version at https://lite.ip2location.com/database/px8-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen 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, datacenters and search engine robots, you may visit https://www.ip2location.com/database/px8-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen for details.

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

Create the table

CREATE DATABASE ip2proxy;
USE ip2proxy;
CREATE TABLE `ip2proxy_px8`(
    `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),
 
    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.CSV'
INTO TABLE
    `ip2proxy_px8`
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_px8 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>';
    }
    else{
        //non-proxy
        echo $ip_address . ' is not a proxy<br>';
    }
?>

 

Was this article helpful?

Related Articles