PHP
  Home arrow PHP arrow Page 4 - Building an IP-to-Country Mapping Application with PHP
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 an IP-to-Country Mapping Application with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-02-03


    Table of Contents:
  • Building an IP-to-Country Mapping Application with PHP
  • Start building an IP-to-country PHP application
  • Transferring the data of the IP-to-country database to the MySQL table
  • Importing records of the IP-to-country database to the MySQL table

  • 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 an IP-to-Country Mapping Application with PHP - Importing records of the IP-to-country database to the MySQL table
    ( Page 4 of 4 )

    In the previous segment, you learned how to create a PHP script which was tasked with importing the contents of the lookup database file into the “iptocountry” MySQL table. Considering that this script reads one line of the file at a time, it’s feasible to create one for illustrative purposes that fetches all of the lines in a single step, by means of the “file()” PHP function.

    Here’s how this sample script looks:


    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('database',$db)){

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

    }

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

    $lines=file('iptocountry.csv');

    foreach($lines as $line){

    $line=str_replace('"','',$line);

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

    if(substr($line,0,1)!='#'){

    $values=explode(",",$line);

    // explode values

    if(count($values)!=1){

    // build query

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

    // run query against MySQL table

    mysql_query($sql);

    }

    }

    }

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Obviously, this modified version of the script built in the previous section doesn’t differ too much from the original, since it utilizes the “file()” PHP native function to read all the lines of the lookup database file at once. Even though this approach can be faster, it requires a web server equipped with more RAM, so if you’re running this script in a slower computer, then I suggest that you use the method discussed in the previous section.

    Regardless of the PHP script that you may want to use for importing the records of the lookup database file into the “ipcountry” MySQL table, at this point the table in question should be filled in with all of the data required for building an IP-to-country mapping program.

    However, this topic will be discussed in depth in the upcoming article of this series. In the meantime, feel free to tweak all the code samples shown in this tutorial, and don’t forget to take a look at the contents of the recently created “iptocountry” MySQL table. You’ll be amazed at how many IP addresses have been mapped to their corresponding countries.

    Final thoughts

    In this initial chapter of the series, I provided you with a quick overview on how to create an IP-to-country mapping MySQL table with PHP. Logically, the data stored on this table is pretty useless if it’s not linked to a concrete application that turns it into meaningful information.

    Therefore, in the forthcoming article I’ll be demonstrating how to build an application like this. Now that you’ve been warned, you don’t have any excuses to miss the next tutorial!



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek