Print

Country Lookup by IP address CSV

What?
So this is an article to serve as documentation for one of the data sources available to download for FREE from this site.

Why?
It's useful to have a CSV list of all the IP ranges and their allocated country. Our use here was to create a refreshable country by IP address list that we can use when parsing our website logs to check where visitors were from.

Note: we also use and love Advanced Web Statistics (awstats) but this was so that you could have your own country lookup script.

The Download
Should contain the following:
  1. create_countriesipranges_table.sql: SQL to create a pre-populated table into a MySQL database.
  2. iana_ipv4_address_space_registry.csv: comma separated values list of all countries, regions and their respective IP ranges.
  3. ip_lookup.php: to use the database
  4. License.txt: Released under GNU/GPL
  5. ReadMe.txt
What is it?
I wanted my own IP lookup script to tell me the country that is allocated a given IP address, this is what I use.

The scripts in the folder you have downloaded are to create a lookup function using a MySQL database and a PHP script. As well as a CSV file equivalent.


How to use:

  1. Download from my site: «Download»
  2. Run the SQL script against a test database,
    1. Modify the table name to use if necessary.
    2. Uncomment the drop statement if running for the second time.
  3. Modify the PHP file to use database login credentials,
    1. $db_host = "localhost"; // Database host name
    2. $db_user = "my_database_user"; // User for database authentication
    3. $db_pass = "my_database_password"; // Password for database authentication
    4. $db_name = "my_database"; // Database name
    5. $db_table = "mytable_countryipranges";// Database table
  4. Upload the PHP file to your website
  5. Test by using a web browser to visit: http://www.mysite.com/ip_lookup.php?ipv4=123.123.123.123 where www.mysite.com is your website where you uploaded this script. // should return China

Any questions, suggestions?
We are looking to add the ability to generate this data using other sources and in time a region by IP for more accuracy.

Feel free to feedback. It's FREE to download unlike others which is probably why I made the generator in the first place but I do like to make people sign up so I can gauge the level of interest.