How to redirect domain according to country IP address in PHP?

PHPServer Side ProgrammingProgramming

The GeoIP extension can be used to find the exact location of an IP address. Apart from this, the geoPlugin class can be downloaded from −

http://www.geoplugin.com/_media/webservices/geoplugin.class.phps

The country code list can be found in the below link −

http://www.geoplugin.com/iso3166

An index.php file can be placed inside the root folder and the below lines of code can be put inside this index file −

<?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 == "AL") {
   header('Location: http://sq.wikipedia.org/');
}
else if ($var_country_code == "NL") {
   header('Location: http://nl.wikipedia.org/');
} else {
   header('Location: http://en.wikipedia.org/');
}
?>

Once the geoplugin class has been downloaded, a new instance is created and given the name ‘geoplugin’. The locate function is called on this instance of the geoplugin class. The same class object’s countryCode is assigned to a variable named ‘var_country_code’. Now, the ‘if’ condition is put to check the letters of the region. Based on this IP address, the redirection to the specific domain takes place.

raja
Published on 09-Apr-2020 11:11:39
Advertisements