Redirect Web Visitors by Country Using PHP and MYSQL Database
Redirect Web Visitors by Country Using PHP and MYSQL Database
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.
PC:
Linux/Unix:
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";
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.