Sunteți pe pagina 1din 3

Redirect Web Visitors By Country Using PHP and MYSQL Database

Hexasoft Development Sdn. Bhd. (645996-K)


1-2-15 Mayang Mall Complex,
Jalan Mayang Pasir 1,
11950 Bandar Bayan Baru,
Penang, Malaysia.
URL: http://www.ip2location.com

{ sales@ip2location.com }

There are times when it is useful to redirect a visitor to different default web page based
on the visitor's country of origin. One practical usage is to redirect visitor to web page
with the language recognized by the visitor. This article shows you how to build up such
a system using PHP (server side scripting language) and MYSQL (IP address to country
lookup database).

Let us take a simple case study. Company XYZ is multi-national company with major
customers from United States and Japan. The company official website is developed in
both English and Japanese languages. The default page is in English language and visitor
can switch to Japanese by changing the default language option. There exists a potential
risk when a Japanese visitor does not understand English and he could not navigate the
web site.

In this tutorial, we use the IP2Location™ IP-Country database to lookup country of


origin from the visitor's IP address. Instead of loading the full database with 50000+
records, we could simplify this tutorial with assumption only two different IP address
ranges in the world. IP addresses 0.0.0.0 - 126.255.255.255 originates from United
States. Meanwhile, IP addresses 127.0.0.0 - 255.255.255.255 originate from Japan.
Here we are creating a database "IP2Location" with table "IPCountry" that consists of
two IP address range records.

Step 1: Create and connect to 'IP2Location' database


mysql> CREATE DATABASE IP2Location
mysql> CONNECT IP2Location

Step 2: Create 'IPCountry' table


mysql> CREATE TABLE IPCountry
--> (
--> ipFROM DOUBLE NOT NULL,
--> ipTO DOUBLE NOT NULL,
--> countrySHORT VARCHAR(2) NOT NULL,
--> countryLONG VARCHAR(100) NOT NULL,
--> PRIMARY KEY(ipFROM, ipTO)
--> );
Step 3. Import the 'ipcountry.csv' database into table 'IPCountry'
mysql> INSERT INTO IPCountry VALUES (0, 2130706431,'US','UNITED STATES');
mysql> INSERT INTO IPCountry VALUES (2130706432,
4294967295,'JP','JAPAN');

The full version of IP-Country database is available for subscription at $49/year


from http://www.ip2location.com. If you have the full version of IP2Location™ IP-
Country database, the import process is easier by using the LOAD DATA feature available
in MYSQL.

PC:

mysql> LOAD DATA INFILE "/IPCountry.csv" INTO TABLE IPCountry FIELDS


TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';

Linux/Unix:

mysql> LOAD DATA INFILE "/IPCountry.csv" INTO TABLE IPCountry FIELDS


TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';

Let's assume the English web page as http://www.google.com and Japanese web
page as http://www.google.co.jp. We implement a simple script index.php to detect
visitor's country of origin. If the visitor is from Japan (with IP address range from
127.0.0.0 - 255.255.255.255), then it redirect him/her to web site in Japan, otherwise
the United States. Simple? Here is the code and the comments serve as explanation
index.php

<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

// Retrieve visitor IP address from server variable REMOTE_ADDR


$ipaddress = getenv(REMOTE_ADDR);

// Convert IP address to IP number for querying database


$ipno = Dot2LongIP($ipaddress);

// Connect to the database server


$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass)
or die("Could not connect to MySQL database");

// Connect to the IP2Location database


mysql_select_db("IP2Location") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between
the valid range
$query = "SELECT * FROM IPCountry WHERE $ipno < = ipTO AND $ipno>=ipFROM";

// Execute SQL query


$result = mysql_query($query) or die("IP2Location Query Failed");

// Retrieve the recordset (only one)


$row = mysql_fetch_object($result);

// Keep the country information into two different variables


$countrySHORT = $row->countrySHORT;
$countryLONG = $row->countryLONG;

// Free recordset and close database connection


mysql_free_result($result); mysql_close($link);

// If the visitors are from JP, redirect them to JP site


if ($countrySHORT == "JP")
{
Header("Location: http://www.google.co.jp");
} else {
// Otherwise, redirect them to US site
Header("Location: http://www.google.com");
}
exit;

// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to


256^4-1)
function Dot2LongIP ($IPaddr) {
if ($IPaddr == "")
{
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 *
256 * 256);
}
}
?>

Place this script as the default script of the web site. All visitors will go through this
screening before redirect to an appropriate web page.

Hexasoft Development Sdn. Bhd. © 2001-2009 All Right Reserved


To obtain permission to reuse or republish this article, please write
to sales@ip2location.com. Republication is welcome for no charge.

S-ar putea să vă placă și