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

Mapping IP addresses to originating countries with a basic geo location application - 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



To be frank, building a PHP application that permits you to determine from which country a user is accessing a web page according to his/her IP address is a straightforward process that can be tackled with minor efforts.

Basically, this procedure is reduced to performing two simple steps: first, capturing the user IP address and second, determining via the "iptocountry" MySQL table which country is tied to that particular address.

However, to clarify this concept a bit further, I coded a sample PHP script (shown below) which accomplishes all the tasks mentioned above. Here it is:


// connect to MySQL server


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


// select database


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


// get user IP address


// Map IP Address to country using 'iptocountry' database

if(!$result=mysql_query("SELECT country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1")){

throw new Exception ('Unable to map IP Address to country.');


// get row from database table


// display country which user is accessing from

echo 'Welcome dear visitor, you are accessing our web site from : '.$row['country'];


catch(Exception $e){

echo $e->getMessage();



See how simple it is to determine from which country a visitor is accessing a web page by means of the "iptocountry" MySQL table? I guess you do! But, to dissipate any doubts, please take a close look at the above code sample.

As you can see, it first connects to MySQL and selects a fictional database, and then it gets the IP address of the user currently visiting the web page. Finally, it determines the country that corresponds to that specific address.

Naturally, this last step is performed by querying the "iptocountry" MySQL table to find out to which range of addresses the user's IP address belongs. In this case, the values stored on the "lower_bound" and "upper_bound" fields are utilized for performing this comparison and for mapping the IP address to its originating country. That's all.

So, suppose that I am the one who is accessing the previous script. It would produce the following output:

'Welcome dear visitor, you are accessing our web site from : ARGENTINA

Pretty neat, right? At this point, you hopefully realized how easy it is to build an IP-to-country program with PHP using a procedural approach. As you might have guessed, however, it's also feasible to develop a similar application by means of the object-oriented paradigm.

Therefore, the last section of this tutorial will be aimed at illustrating how to create a lookup program by using a basic MySQL abstraction class.

The complete details regarding the definition of this class will be revealed in the following section. Please click on the link that appears below and read the next few lines.

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