PHP
  Home arrow PHP arrow Page 3 - 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 - Transferring the data of the IP-to-country database to the MySQL table
    ( Page 3 of 4 )

    As you may have guessed, there are many ways to create a PHP script that transfers the contents of the CSV lookup database to the MySQL table defined in the previous segment. But in this situation, since the number of records to be imported is fairly huge, I’m going to build a small file reading program, based on the script developed by Matthew Pennell here.

    In short, the PHP script that reads the content of the lookup database and imports it to the previous MySQL table looks like this:


    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.');

    }

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

    set_time_limit(360);

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

    if(!$fp=fopen('iptocountry.csv','r')){

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

    }

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

    while(!feof($fp)){

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

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

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

    // explode values

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

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

    }


    Although it may seen a bit complex at first, the logic that drives the above script is quite simple to grab. As shown previously, the script starts by connecting to MySQL and selecting a database (naturally, you should change these illustrative parameters and use the ones that correspond to your MySQL settings). It then  opens the lookup database file, and reads one line at a time.

    Finally, if the file line being read isn’t a comment, it’s used to build an “INSERT” query, which imports the lookup data into the “iptocountry” MySQL table. It’s that simple, really.

    In addition, there’s an important detail worth stressing here with reference to the way that the previous script works. As you may have noticed, it extends the timeout default value of PHP to 360 seconds via the “set_time_limit()” function. Here's the reason: since the number of records to be imported is large (over 82,000 rows), this prevents the PHP engine from triggering a time out error when performing this process in a slow machine. Of course, if you’re running the script in a turbo-charged computer, you won’t suffer this problem.

    All in all, if everything worked as expected, then by this time the “iptocountry” MySQL table should have been populated with all of the records imported from the lookup database file. The following screen capture shows how the table looks when viewed with the MySQL Query Browser:



    Now things are getting really interesting, aren’t they? At the moment, the lookup MySQL table contains all the data required to build an IP-to-country mapping application. As I said before, my purpose here is to build this application specifically with PHP, but undoubtedly you can use another programming language, as long as it offers support for MySQL.

    In the following section, I’m going to code a slightly different version of the prior PHP script, which will read all of the lines of the database file at once, implying that it should only be utilized with computers with a huge amount of RAM.

    To see how this modified script will be developed, please jump forward and read the segment to come. 



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