PHP
  Home arrow PHP arrow Page 4 - Generating Web Pages in Multiple Languages with a PHP 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

Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-02-18


    Table of Contents:
  • Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application
  • Review: building IP-to-country mapping applications
  • Building web pages in different languages
  • Building a simple front controller

  • 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


    Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application - Building a simple front controller
    ( Page 4 of 4 )

    As I said in the section that you just read, it's possible to generate complete web pages in different languages through the use of a simple front controller. Again, I'm not saying that this is the only way to accomplish this, but it's one of the simplest to use.

    The following code sample shows how to utilize a basic PHP front controller to build the header, body and footer sections of a web page, depending on the countries from which users are accessing the page:


    // define 'MySQL' class

    class MySQL{

    private $mysqli;

    private $result;

    public function __construct($host='localhost',$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;

    }

    }


    // this is the front controller

    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 three_chars_country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1");

    // get row from database table

    $row=$db->fetchRow();

    // get 3-char country code

    $code=$row['three_chars_country'];

    // generate web page depending on country code

    require_once 'header_'.$code.'.php';

    require_once 'body_'.$code.'.php';

    require_once 'footer_'.$code.'.php';

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    That was very easy to code and read! As shown before, the lookup MySQL table is used to retrieve the 3-char country code that corresponds to the user IP, and then this value is utilized to generate the appropriate web page sections via a few PHP includes.

    Obviously, this is a quick and dirty approach that will let you build web documents in a multi-lingual fashion, but certainly it isn't the only one. Anyway, the above example should help you to start building IP-to-country mapping applications with PHP very quickly.

    Final thoughts

    In this third installment of the series, you learned how to build dynamic web pages in different languages by using the previous "iptocountry" lookup MySQL table. As you saw before, generating web documents by way of a multi-language platform is indeed a straightforward process, since the hard work is charged mostly to this table.

    In the last article, I'll be discussing how to build a simple IP-to-country mapping application by using the Code Igniter PHP framework, in this way taking advantage of the functionality given by the Model-View-Controller pattern.

    Now that you've been warned, you won't want to miss the final chapter of this series!



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