PHP
  Home arrow PHP arrow Page 3 - Building Sample Programs for an IP-to-country Mapping Application
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building Sample Programs for an IP-to-country Mapping Application
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-02-10


    Table of Contents:
  • Building Sample Programs for an IP-to-country Mapping Application
  • Review: building an IP-to-country mapping MySQL table with a lookup database file
  • Mapping IP addresses to originating countries with a basic geo location application
  • Developing an object-oriented geo-location PHP application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building Sample Programs for an IP-to-country Mapping Application - Mapping IP addresses to originating countries with a basic geo location application
    ( Page 3 of 4 )

    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:


    try{

    // connect to MySQL server

    if(!$db=mysql_connect('host','user','password')){

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

    }

    // select database

    if(!mysql_select_db('alejandro',$db)){

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

    }

    // get user IP address

    $ip=sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));

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

    $row=mysql_fetch_array($result);

    // 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();

    exit();


    }


    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek