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 −

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

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

$geoplugin = new geoPlugin();
// create a variable for the country code
$var_country_code = $geoplugin->countryCode;
// redirect based on country code:
if ($var_country_code == "AL") {
else if ($var_country_code == "NL") {
} else {

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.

Updated on 09-Apr-2020 11:12:10