PHP
  Home arrow PHP arrow Page 4 - 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 - Developing an object-oriented geo-location PHP application
    ( Page 4 of 4 )

    In the previous segment, I showed you how to build a procedural PHP script which was capable of mapping users' IP addresses to their corresponding countries via the "iptocountry" MySQL table. Thus, in this last section of the article, I'd like to demonstrate how this same task can be performed by way of an object-oriented approach.

    To do this, I'm simply going to create a basic MySQL abstraction class, which will carry out the IP-to-country mapping process by means of a simple interface. The signature of this class, along with an example of how to use it, is listed below:


    // define 'MySQL' class

    class MySQL{

    private $mysqli;

    private $result;

    public function __construct($host='host',$user='user',$password='password',$database='database'){

    // connect to MySQL and select database

    $this->mysqli=new mysqli($host,$user,$password,$database);

    if(mysqli_connect_errno()){

    throw new Exception('Error connecting to MySQL: '.$this->mysqli->error);

    }

    }

    // run SQL query

    public function query($query){

    if(!$this->result=$this->mysqli->query($query)){

    throw new Exception('Error running SQL query: '.$this->mysqli->error);

    }

    }

    // fetch one row

    public function fetchRow(){

    while($row=$this->result->fetch_assoc()){

    return $row;

    }

    return false;

    }

    }


    try{

    // connect to MySQL and select database

    $db=new MySQL('host','user','password','database');

    // get user IP address

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

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

    $db->query("SELECT country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1");

    // get row from database table

    $row=$db->fetchRow();

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

    }


    If you develop object-oriented applications with PHP on a frequent basis, then you'll grasp the logic that drives the above script in a snap. In this particular situation, users' IP addresses are directly mapped to their respective countries by performing the same SQL query used with the procedural example. In this case the "iptocountry" table is queried by means of the "MySQL" accessing class shown before.

    Finally, as usual with many of my articles on PHP development, feel free to improve all of the code samples included in this article, and use them as a guide for developing different, more creative IP-to-country mapping applications.

    Final thoughts

    Over the second installment of this series, you hopefully learned how to take advantage of the "iptocountry" MySQL table created in the preceding tutorial to develop a couple of geo-location applications with PHP.

    First I used a procedural approach to build the application, and then, in the second example, I showed you how to build it using the object-oriented paradigm. Of course, both programs offer practically the same level of functionality, so feel free to pick the one that best suits your needs.

    In the upcoming tutorial, I'll be discussing how to generate dynamic web pages in different languages, naturally by using the previous MySQL lookup table. Therefore, now that you know what the next article will be about, you can't miss it!



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