Home arrow PHP arrow Page 3 - Using Code Igniter to Build an IP-to-Country Mapping Application

Building a simple IP-to-country mapping application with the Code Igniter framework - PHP

Adding geo-location capabilities to a web site isnít too hard; itís possible to develop an IP-to-country mapping program with PHP, which can be incorporated into a site in a few easy steps. If youíre interested in learning how to build this application, then keep reading. This is the final installment of a four-part series that covers developing an IP-to-country mapping application with PHP.

TABLE OF CONTENTS:
  1. Using Code Igniter to Build an IP-to-Country Mapping Application
  2. Review: Building dynamic web pages in distinct languages using the iptocountry MySQL table
  3. Building a simple IP-to-country mapping application with the Code Igniter framework
  4. Finishing the IP-to-country mapping application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 24, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If you're not familiar with the Code Igniter framework, don't worry; the program that I'm going to build in the next few lines will be pretty straightforward. In simple terms, any web application developed with this framework will be composed of three separate modules, based on the Model-View-Controller approach: first the controller, which is charged with handling application logic; then the model, which handles business logic, generally in the form of database tables or a data access layer; and finally, one or more views, usually represented by a set of HTML pages.

So, first I'm going to define a "Products" controller. It will handle the logic required to generate a fictional, multi-lingual "products" web page section. Finally, I'm going to create a view file, tasked with displaying these products as a simple web document.

The prototype of the controller class looks like this:


<?php


// define 'Products' controller class

class Products extends Controller{

function Products(){

// load controller parent

parent::Controller();

// load database class and connect to MySQL

$this->load->database();

}

// display products list

function index(){

// get user IP address and check to see if it's valid

if($this->input->valid_ip($this->input->server('REMOTE_ADDR'))){

$ip= sprintf('%u',ip2long($this->input->server('REMOTE_ADDR')));

// get 3-char country code

$this->db->select('three_chars_country');

$code=$this->db->get_where('iptocountry',array('lower_bound <=' => $ip,'upper_bound =>' => $ip);

// get products list from country code-based MySQL table

$products=$this->db->get('products_'.$code);

// load 'products' view depending on country code

$this->load->view('products_'.code'.php',$products);

}

}

}

?> 


To put it simply, the above "Products" controller starts loading the database class that comes bundled with Code Igniter, which is used within the "index()" method, to fetch the contents from different "products" MySQL tables, depending on the countries from which users are accessing this fictional web site.

In the end, the controller finishes its execution by loading the appropriate view file and embedding the product-related data into it. This short example should give you an idea of how to generate a "products" web page in several languages using Code Igniter.

However, the missing piece of this schema is the view file that renders these products in the form of a basic web document. Thus, in the final section of this tutorial I'm going to build this file.

Therefore, click on the link that appears below and read the following segment. We're almost done!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: