Source link: http://ekstreme.com/geotargeting/flags-comments.php
Requirements
- A database of worldwide IP addresses. (I’ll show you where to get one in a second.)
- A database, like MySQL, and a server-side programming language, like PHP. The examples in this tutorial are PHP/MySQL based, but any combination should work.
- Optionally, a set of country flags – again, I’ll show where you get them.
Geo IP database: setup
The first thing you need is a database of world wide IP addresses. A freely available one is MaxMind’s GeoLite Country Database. Download the CSV version, not the binary format version.
A note about the database:
- It is updated once a month, so you can (and should) update your copy frequently.
- It is claimed to be 97% accurate. This is sufficient for most users, but if you want a more accurate version, download the MaxMind GeoIP Country Database version, which is not free.
Once you download the IP database in CSV format, you need to upload it into your MySQL database. Here are the instructions:
- Create a new table in your database; call it IPCountries.
- In the IPCountries table, create 6 (six) new fields.
- The fields are:
Field Type IP_START VARCHAR, length 50 IP_END VARCHAR, length 50 IP_FROM Double IP_TO Double COUNTRY_CODE2 Char, length 2 COUNTRY_NAME VARCHAR, length 50 - Import the CSV file into the MySQL table. If you are using phpMyAdmin, at the very bottom of the table view is a link that says ‘Insert data from a text file into the table’. Click that and then make sure the settings are the following:
Setting Value Fields terminated by , (a comma) Fields enclosed by ” (a double-quote) Lines terminated by \n (a linefeed)