HomePHP Page 2 - Building an IP-to-Country Mapping Application with PHP
Start building an IP-to-country PHP application - PHP
If you have content in several different languages, your visitors probably speak several different languages as well. Wouldn't it be nice to serve them content from your site in their native language, based on the country from which they hail? You can, even if you're a small company, with an application that tells you a visitor's country based on their IP address. This four-part article series will show you how to build the application and incorporate it into your web site.
Before you start learning how to build an IP-to-country application with PHP, you should download the source lookup database from the Webnet77’s web site. Then, point your browser to http://software77.net/cgi-bin/ip-country/geo-ip.pl and proceed to download the ZIP file that contains a CSV version of the database to your machine (make sure to read the corresponding term and conditions before doing this).
Got that file uncompressed on your computer? Good. Now, it’s time to use it to transfer all the records contained in the source database to a new MySQL table, so they can be accessed with PHP. To do so, you’ll need to rename the downloaded file to “iptocountry.csv,” and then create the table in question in the following way:
CREATE TABLE ‘iptocountry’ (
‘lower_bound’ INT(11) UNSIGNED NOT NULL default '0', ’upper_bound’ INT(11) UNSIGNED NOT NULL default '0', ’owner’ VARCHAR(20) NOT NULL, ’last_updated’ INT(11) DEFAULT NULL, ’two_chars_country’ CHAR(2) NOT NULL default '', ’three_chars_country’ CHAR(3) NOT NULL default '', ’country’ VARCHAR(100) NOT NULL default ''
);
As shown by the above SQL code, the structure of the previous MySQL “iptocountry” table is comprised of seven fields, whose names speak for themselves about the type of data that they’ll house. In this case, the two first fields will store a lower IP boundary and an upper IP boundary respectively, represented as numbers (not as dot-separated octets). Then, there are a couple of additional fields, called “owner” and “last_updated,” where the first one represents the registry that owns that range of IP addresses, and the second field, not surprisingly, is the last time the range was updated.
In addition, the last three fields of the table will be used for storing the name of the country that corresponds to that range of IP addresses in the form of two-character and three-character strings respectively, as well as the country’s full name. Not too hard to grasp, right?
So far, so good. At this point, you’ve created a simple MySQL table, which will store all of the data required for building an IP-to-country mapping program with PHP. It’s quite possible, however, that at this moment you’re wondering how this data will be imported to this table.
Well, since the source lookup database is actually structured as a CSV file (comma-separated values), it’s necessary to build a simple PHP script that imports the data from that file to the “iptocountry” MySQL table.
Thus, if you’re interested in learning the full details of how this script will be set up, please click on the link that appears below and read the section to come.