Redirect Web Visitors By Country Using PHP and MYSQL

IP geolocation Database Products

IP Address Country Database FAQ F.A.Q.
Purchase IP2Country Database Purchase
Downloads Downloads
IP2Country Database - Technical Details Technical Details
Contact us Contact us

IP Address Locator
IP Geolocation Articles & Tutorial (Free)

  IP2Country Database   IP Redirection PHP script  

How to Redirect users based on their IP Using Php and MySQL

I’ll illustrate how to use PHP (server side scripting language) and MYSQL (IP address to country lookup database) to redirect your web site visitors to a different page based on their IP addresses.

The MySQL Part
1>Create a MySQL database. I like phpMyAdmin, but use whatever method you are comfortable with. Name the database ip2country.

Create and connect to 'ip2country' database
 mysql> CREATE DATABASE ip2country
 mysql> CONNECT ip2country

2>Create 'ip2country' table 

CREATE TABLE `ip2country` (
`begin_ip` varchar(15) NOT NULL default '',
`end_ip` varchar(15) NOT NULL default '',
`begin_number` double NOT NULL default '0',
`end_number` double NOT NULL default '0',
`countryCode` char(2) NOT NULL default '',
`countryName` varchar(64) NOT NULL default '',
PRIMARY KEY (`begin_number`,`end_number`)
)

3>Import the 'ip2country.csv' database into table 'ip2country' 

It is easier by using the LOAD DATA feature available in MYSQL.
 mysql> LOAD DATA INFILE "/ip2country.csv" INTO TABLE IPCountry FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r'; 

IP-Country mapping Database Available Formats:
1>Comma-separated values (CSV) ( Sample data)
2>Plain text For Target Country by IP Address
3>MySQL dump format (SQL)
4>MS Access database (.mdb)
5>Ip2country database For phpAdsNew (New Format)

If you have the full version of IP Country mapping database,you can easy Importing IP2Country MySQL dump file into MySQL.

The PHP Part
Let's assume the client is looking to evaluate 3 countries (UK, USA and Ireland) . if the visitor in the the USA it will go to us.html and if in the UK it will go to uk.html, otherwise Redirect to uk.html

1>Detect the real IP address of visitor
<?if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR); }
?>

2>Redirect Visitors By Country (PHP Script)

<?php
// Get the real IP address of the visitor
if (getenv(HTTP_X_FORWARDED_FOR))
{ $ip = getenv(HTTP_X_FORWARDED_FOR); }
else { $ip = getenv(REMOTE_ADDR); }

// Convert IP address to IP number for querying database
$ipn = anp_ip2long($ip);

// Establish a database connection (adjust address, username, and password)
$mysql_server = "localhost";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

// 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 ip2country database 
mysql_select_db("ip2country") or die("Could not select database"); 

// Create a query string
$country_query = "SELECT countrycode FROM ip2country WHERE end_number >=$ipn AND begin_number<=$ipn";

// Execute the query
$country_exec = mysql_query($country_query);
// Fetch the record set into an array
$ccode_array = mysql_fetch_array($country_exec);
// Close the database connection
mysql_close($link);

// Get the country code from the array and save it as a variable
$countrycode = $ccode_array['countrycode'];
// If the visitors are from US, redirect them to US Web Page
if ($countrycode == "US")
{
Header("Location: http://www.yourstore.com/us.html");
} else {
if ($countrycode == "UK")
{
Header("Location: http://www.yourstore.com/uk.html");
}
// Otherwise, redirect them to IE Web Page
else { Header("Location: http://www.yourstore.com/ie.html");}
}
exit;

// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number
function anp_ip2long ($IPaddr)
{
if ($IPaddr == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] * 16777216);
}
}
?>


Copyright ©2001-2017 Jgsoft Associates. All rights reserved.