User's Guide
CSV File Format
The CSV file contains four fields:
- Begining of IP address range
- Ending of IP address range
- Two-character country code based on ISO 3166
- Three-character country code based on ISO 3166
- Country name based on ISO 3166
This is a sample of how the CSV file is structured:
"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
"0050331648","0083886079","US","USA","UNITED STATES"
"0094585424","0094585439","SE","SWE","SWEDEN"
...
Note: All IP addresses are represented as IP Numbers which is the numeric representation of the dotted IP address.
You can import this data into any database by creating a table with the following fields:
| FIELD |
DATA TYPE |
FIELD DESCRIPTION |
| IP_FROM |
NUMERICAL (DOUBLE) |
Beginning of IP address range. |
| IP_TO |
NUMERICAL (DOUBLE) |
Ending of IP address range. |
| COUNTRY_CODE2 |
CHAR(2) |
Two-character country code based on ISO 3166. |
| COUNTRY_CODE3 |
CHAR(3) |
Three-character country code based on ISO 3166. |
| COUNTRY_NAME |
VARCHAR(50) |
Country name based on ISO 3166 |
You can query the above table, after you have imported the data into it, to find the country of a corresponding IP Number by issuing the following Select statement:
SELECT COUNTRY_NAME FROM <TableName> WHERE IP_FROM <= IP Number and IP_TO >= IP Number
IP Number
The IP_FROM and IP_TO fields of the IP-to-Country Database are numeric representations of the dotted IP address.
The formula to convert an IP Address of the form A.B.C.D to an IP Number is:
IP Number = A x (256*256*256) + B x (256*256) + C x 256 + D
Which is the same as:
IP Number = A x 16777216 + B x 65536 + C x 256 + D
In PHP 4 you can use the following to convert a dotted IP Address to its corresponding IP Number:
$ip_number = sprintf("%u", ip2long($dotted_ip_address));
and this to convert IP Number to its corresponding dotted IP Address:
$dotted_ip_address = long2ip($ip_number);
Example code to detect country
<?
//---------------------------------------------------
// Sample code to display Visitor Country information
// PHP 4
//---------------------------------------------------
// Establishing a database connection
$dbh=mysql_connect("localhost:3306","$MYSQL_USERNAME","$MYSQL_PASSWORD");
mysql_select_db("$MYSQL_DBNAME");
// Query for getting visitor countrycode
$country_query = "SELECT country_code2,country_name FROM iptoc ".
"WHERE IP_FROM<=inet_aton('$REMOTE_ADDR') ".
"AND IP_TO>=inet_aton('$REMOTE_ADDR') ";
// Executing above query
$country_exec = mysql_query($country_query);
// Fetching the record set into an array
$ccode_array=mysql_fetch_array($country_exec);
// getting the country code from the array
$country_code=$ccode_array['country_code2'];
// getting the country name from the array
$country_name=$ccode_array['country_name'];
// Display the Visitor coountry information
echo "$country_code - $country_name";
// Closing the database connection
mysql_close($dbh);
?>
Web Service
NOTE: Read this first HTTP GET based Web Service is DOWN for now.
IP-to-Country provides a HTTP GET based Web Service to access country information for a particular IP Address.
The format of the request can be one of the following:
1) http://ip-to-country.directi.com/country/name/IP_ADDRESS
2) http://ip-to-country.directi.com/country/code2/IP_ADDRESS
3) http://ip-to-country.directi.com/country/code3/IP_ADDRESS
In the above URL's IP_ADDRESS has to be replaced with the IP address for which you want to retrive country information.
The first request will return the name of the country while the next two will return the 2 letter and 3 letter country code as
defined in the ISO 3166 Country Codes Standard respectively.
In PHP, you can make the first request using the following code snippet:
$handle = fopen("http://ip-to-country.directi.com/country/name/".$ip, 'r');
echo fgets($handle, 4096);
fclose($handle);
The other two requests can be achieved by simply replacing "name" with either "code2" or "code3".