Sometimes you need to redirect visitors based on the country they come from. Maxmind provides a great country/IP address database in binary format. The following example will show you how redirect visitors from countries which are not on the "whitelist".
Download the latest Geo data and the class file here.
Copy the class file and the GeoIP data file into the same directory where the page is located (most of the time the root directory)
Include the class file inside your page and add the following code to request the country using the following code (below the include statements):
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
Define some country codes as the "whitelist":
$my_countries = array('us', 'ca', 'gb', 'fr', 'de', 'nl');
Next place the following code below the $mycountries array:
if (!in_array(strtolower($country), $my_countries)) {
header('Location: some URL...');
exit;
}