The IP-to-Country Handbook
General information about IP-to-Country
The IP-to-Country Database and Tools can be used for:
- Real Time Geo-Locating (Country)
- Provide Location Aware Content (language, currency, etc.)
- Corporate Websites/E-commerce (think global, act local)
- Fraud Detection (Credit Card Fraud, etc.)
- On-line Campaign (targeted Banner Ads)
- Digital Rights Management
- Web Server Log Analysis
- Auto-selection of fields on forms (country, currency,language, etc.)
- Filter access on basis of originating country
- Spam Filtering
Although not 100% accurate, the IP-to-Country Database is about 98% accurate on country recognition. The main reasons for this lie in the existence of dynamic IP addresses and Internet access through proxy servers. Also, it should be noted that the IP-to-Country Database seeks to indicate the country where resources were first allocated or assigned and are not an authoritative statement of the location in which any specific resource may currently be in use. These cases are very difficult and sometimes impossible to map. However at this moment the IP-to-Country Database is by far the most accurate way to determine the location of Internet users in real-time.
Downloads
IP-to-Country Database License
Copyright (c) 2003 Direct Information Pvt. Ltd. All Rights Reserved.
All usage, reproduction, modification and derivative works created from, and distribution and publication of the IP-to-Country Database and your derivative works thereof must keep intact all copyright notices and give credit by displaying the following acknowledgment by replacing 'work' with one of the following: script, product, page, service or application:
"This 'work' uses the IP-to-Country Database
provided by WebHosting.Info (http://www.webhosting.info),
available from http://ip-to-country.webhosting.info."
BECAUSE THE DATABASE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE DATABASE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE DATABASE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE OR ANY WARRANTIES REGARDING THE CONTENTS OR ACCURACY OF THE WORK.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE DATABASE AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE DATABASE, EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
RIR Licenses
Some parts of the IP-to-Country Database are derived from APNIC, ARIN, LACNIC and RIPE databases whose copyright details are given below:
APNIC CONDITIONS OF USE:
The files are freely available for download and use on the condition that APNIC will not be held responsible for any loss or damage arising from the application of the information contained in these reports.
APNIC endeavours to the best of its ability to ensure the accuracy of these reports; however, APNIC makes no guarantee in this regard.
In particular, it should be noted that these reports seek to indicate the country where resources were first allocated or assigned. It is not intended that these reports be considered as an authoritative statement of the location in which any specific resource may currently be in use.
ARIN database copyright:
Copyright (c) American Registry for Internet Numbers. All rights reserved.
RIPE database copyright:
Copyright (c) by RIPE NCC
Restricted rights.
Except for agreed Internet operational purposes, no part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, recording, or otherwise, without prior permission of the RIPE NCC on behalf of the copyright holders. Any use of this material to target advertising or similar activities is explicitly forbidden and may be prosecuted. The RIPE NCC requests to be notified of any such activities or suspicions thereof.
LACNIC database copyright:
Copyright (c) LACNIC
Sites/Projects that use the IP-to-Country Database
This is a list of websites/projects that use the IP-to-Country Database. If you want to be listed here, all you have to do is tell us by posting in the "Do you use the IP-to-Country Database?" forum page with the following information.
PHP.net - The homepage of the widely-used general-purpose scripting language PHP. PHP.net uses the IP-to-Country Database for suggesting the nearest mirror site to its users, personalizing user experience and load balancing.
http://danzcontrib.free.fr/en/utilisateurs.php - The site gives examples of website captures using httrack or winhttrack, it is about why there are problems to mirror a site with CSS, Flash files, javascript, php, asp, cfm and java applets and how to fix some of them. The page using the IP-to-Country Database illustrates the use of a php script to defeat a web copier.
Lookup IP - Server backend/secured server front-end for tracking users information (country logged in from, user logged in as, and date). works passively on top of any other PHP script/site. logs all site hits that meet set criteria, 2nd server downloads logs.
Information required for getting listed
The following information is required for getting listed in the "Sites/Projects that use the IP-to-Country Database" page in The IP-to-Country Handbook.
- Name of the website/project
- The URL for the website/project
- A short description website/project and how it uses the IP-to-Country Database
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".Resources
This is a list of community contributed code, scripts & images.
Flags
ChangeLog
A list of changes made to the website and confirmed bugs in the database.