user1561167
user1561167

Reputation: 1

MaxMind GeoIP redirects everything?

i would like to redirect users from specific countries to another language. Therefore I have uploaded MaxMind's GeoIPv6.dat and the geoip.inc in the folder of my webpage.

I am using following script in the header of index.php:

<?php

require_once('geoip.inc');

$gi = geoip_open('GeoIPv6.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

$my_countries = array('AT', 'DE');
if (!in_array(strtolower($country), $my_countries))
{
header('Location: http://www.de.mywebsite.com');
}
else
{
header('Location: http://www.mywebsite.com');
}
?> 

Strangely, the script forwards everyone to de.mywebsite.com? Why is that? How can I solve this issue?

Thanks for your help!!!

Upvotes: 0

Views: 488

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53563

You're converting the country code to lowercase, but seeding your array with uppercase:

$my_countries = array('AT', 'DE');
if (!in_array(strtolower($country), $my_countries))

Upvotes: 1

Pekka
Pekka

Reputation: 449475

You are redirecting everyone except visitors from Germany and Austria to the German site.

You probably want

if (in_array(strtolower($country), $my_countries))

without the exclamation mark that inverts the condition.

Oh, and also what @Alex says in his answer.

Upvotes: 1

Related Questions