Home arrow PHP arrow Page 2 - Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application

Review: building IP-to-country mapping applications - PHP

If you’re a PHP developer interested in learning how to develop an IP-to-country application that can be integrated into any existing web site, then look no further. Welcome to the third part of a series on developing an IP-to-country mapping application with PHP. Made up of four approachable tutorials, this series teaches you how to create a program like this by using a single MySQL lookup table, and shows you how to use this program to enrich your own web sites with geo-location capabilities.

TABLE OF CONTENTS:
  1. Generating Web Pages in Multiple Languages with a PHP IP-to-Country Mapping Application
  2. Review: building IP-to-country mapping applications
  3. Building web pages in different languages
  4. Building a simple front controller
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
February 18, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I proceed to explain how to build dynamic web pages in different languages using the lookup MySQL table created in the previous article of the series, I will quickly review the two practical examples developed in that tutorial. They demonstrated how to utilize the table to map IP addresses of users to their respective countries.

Having said that, here's how the examples originally looked:


(procedural approach)


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

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

}

// get user IP address

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

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

if(!$result=mysql_query("SELECT country FROM iptocountry WHERE lower_bound <=$ip AND upper_bound >= $ip LIMIT 1")){

throw new Exception ('Unable to map IP Address to country.');

}

// get row from database table

$row=mysql_fetch_array($result);

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


}



(object-oriented approach)


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

}


As depicted above, it's really simple to develop an IP-to-country mapping program with PHP, since this procedure only requires querying the "iptocountry" MySQL table based on the IP address collected from the user's machine. In the first case, the mapping process is performed via a procedural script, while the second example utilizes a rudimentary MySQL abstraction class.

So far, so good. At this point, you've grasped the logic that drives the hands-on examples. It's time to see how this MySQL lookup table can be utilized for generating web pages in different languages.

This topic will be discussed in depth in the following section. So click on the link that appears below and keep reading. 



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