Home arrow PHP arrow Page 2 - Building Sample Programs for an IP-to-country Mapping Application

Review: building an IP-to-country mapping MySQL table with a lookup database file - PHP

Undeniably, the ability to determine from which countries visitors are accessing a particular web site can be useful, and not only for statistical purposes. With this information at your disposal, itís possible to instruct the site to deliver its content in different languages, a feature that users worldwide will appreciate. This second part of a four-part series moves you in that direction with some sample programs.

  1. Building Sample Programs for an IP-to-country Mapping Application
  2. Review: building an IP-to-country mapping MySQL table with a lookup database file
  3. Mapping IP addresses to originating countries with a basic geo location application
  4. Developing an object-oriented geo-location PHP application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 10, 2009

print this article



In case you haven't had the chance to read the preceding article of this series, in the next few lines I'm going to list for you all of the source files required for building an IP-to-country MySQL table. I will be using the records of the source lookup database, previously downloaded from Webnet77's web site.

That being said, here are the corresponding definitions for each of these files:

(definition of 'create_ip_to_country_table.sql' file)

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 ''


(definition of 'populate_table.php' file)



// connect to MySQL server


throw new Exception ('Error connecting to the server.');


// select database


throw new Exception ('Error selecting database.');


// set timeout limit (handy when working with slower machines)


// open 'iptocountry.csv' file for further reading


throw new Exception ('Error reading source file.');


// read file and populate 'iptocountry' MySQL table with each line of file



// check to see if current line is not a comment


// explode values



// build query

$sql="INSERT INTO iptocountry VALUES ('".implode("','",$values)."')";

// run query against MySQL table






catch(Exception $e){

echo $e->getMessage();




The two files listed above contain all the source code required for creating an IP-to-country mapping MySQL table. In this case, the first SQL file is responsible for creating the structure of the table in question, while the second one is tasked with importing the records of the source lookup database, which was previously downloaded as a CSV file from Webnet77's web site.

Now that you remember how the previous source files do their things, it's time to build a pair of simple PHP applications. These applications will take advantage of the functionality of the "iptocountry" MySQL table created in the previous tutorial to map the IP addresses of different users to their originating countries.

To learn how these sample PHP programs will be developed, please click on the link that appears below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: