ip2location facebbook  ip2location twitter  ip2location google+ ip2location github

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 PX4 database as the example, but the logic can be used for other IP2Proxy PX database query, namely PX1, PX2 and PX3.

Set up the IP2Proxy PX4 database

Firstly, you need to setup the proxy database for the query. You can download a free PX4 LITE version at http://lite.ip2location.com/database/px4-ip-proxytype-country-region-city-isp for trying. However, please take note that the LITE version is only containing the PUB (public proxies) records only. If you are looking for complete proxy database, including Public Proxies, Web Proxies, VPN, TOR and database centers, you may visit http://www.ip2location.com/databases/px4-ip-proxytype-country-region-city-isp for details.

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

Create the table

CREATE DATABASE ip2proxy;
USE ip2proxy;
CREATE TABLE `ip2proxy_px4`(
	`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),
	
	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.CSV'
INTO TABLE
	`ip2proxy_px4`
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', '123456', '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_px4 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 . '
'; echo 'Proxy Type: ' . $row['proxy_type'] . '
'; echo 'Country Code: ' . $row['country_code'] . '
'; echo 'Country Name: ' . $row['country_name'] . '
'; echo 'Region Name: ' . $row['region_name'] . '
'; echo 'City Name: ' . $row['city_name'] . '
'; echo 'ISP: ' . $row['isp'] . '
'; } else{ //non-proxy echo $ip_address . ' is not a proxy
'; } ?>

Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.