Home > Article > Backend Development > How to redirect domain name based on country IP address in PHP?
The GeoIP extension can be used to find the precise location of an IP address. In addition to this, the geoPlugin class can be downloaded from −.
http://www.geoplugin.com/_media/webservices/geoplugin.class.phps
The list of country codes can be found at the following link−
http://www.geoplugin.com/iso3166
You can place an index.php file in the root directory and place the following lines of code in 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 is downloaded, a new instance will be created and named 'geoplugin'. Call the locate function on this instance of the geoplugin class. Assign the countryCode of the same class object to a variable named 'var_country_code'. Now, use 'if' condition to check the letters of the region. Based on this IP address, redirection to a specific domain name will be performed.
The above is the detailed content of How to redirect domain name based on country IP address in PHP?. For more information, please follow other related articles on the PHP Chinese website!