How to redirect website visitors by country IP address in PHP
We will explore how to retrieve the visitor's IP address, determine their country using a third-party service, and then redirect them to the appropriate regional site

1 . Download geo location php file here
2. The country code list can be found in the link here
3. index.php file code
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "IN") {
header('Location: http://in.example.com/');
}
else if ($var_country_code == "PK") {
header('Location: http://pk.example.com/');
} else {
header('Location: http://us.example.com/');
}
?>
What's Your Reaction?






