Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   PHP - geoIP country redirect (http://www.go4expert.com/forums/php-geoip-country-redirect-t25035/)

webdevel 20Feb2011 07:47

PHP - geoIP country redirect
 
I could really use some help with a problem I'm having. I've read as many posts as i can find on this topic but unfortunatly I still cant get it to work.

I would like to redirect visitors to different sites depending on their country. I just can't get this to work. Any help would be very much appreciated. Here is what I've got:

Code:

<?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
 
if ($country == 'ca') {
header('Location: example.ca');
}
if ($country == 'nz') {
header('Location: example.co.nz');
}
if ($country == 'uk') {
header('Location: example.co.uk');
}
if ($country == 'us') {
header('Location: example.info');
}
?>


lionaneesh 21Feb2011 18:03

Re: PHP - geoIP country redirect
 
Quote:

Originally Posted by webdevel (Post 79602)
I could really use some help with a problem I'm having. I've read as many posts as i can find on this topic but unfortunatly I still cant get it to work.

I would like to redirect visitors to different sites depending on their country. I just can't get this to work. Any help would be very much appreciated. Here is what I've got:

Code:

<?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
 
if ($country == 'ca') {
header('Location: example.ca');
}
if ($country == 'nz') {
header('Location: example.co.nz');
}
if ($country == 'uk') {
header('Location: example.co.uk');
}
if ($country == 'us') {
header('Location: example.info');
}
?>


The code looks perfectly fine to me...
Whats the error can you specify..

webdevel 23Feb2011 04:38

Re: PHP - geoIP country redirect
 
I figured out that I could fix my problem with the following:

<?php
require_once('geoip.inc');
$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
$my_countries = 'us';
if (strtolower($country) == $my_countries) {
header('Location: example.us');
}
$my_countriess = 'nz';
if (strtolower($country) == $my_countriess) {
header('Location: example.co.nz);
}
$my_countriesss = 'uk';
if (strtolower($country) == $my_countriesss) {
header('Location: example.co.uk');
}
$my_countriessss = 'ca';
if (strtolower($country) == $my_countriessss) {
header('Location: example.ca');
}
?>

Thanks anyway for your help :)

etiergjt 24Mar2011 07:08

Re: PHP - geoIP country redirect
 
hmm,where did you find that ip range file?can you send me a copy?

webdevel 24Mar2011 09:31

Re: PHP - geoIP country redirect
 
Go to maxmind.com

etiergjt 24Mar2011 09:39

Re: PHP - geoIP country redirect
 
Quote:

Originally Posted by webdevel (Post 81005)
Go to maxmind.com

thanks for your reply,but it's expensive,any cheaper solution for this?

webdevel 24Mar2011 09:57

Re: PHP - geoIP country redirect
 
It's free. What you want to download is GeoLite Country. It's 99.5% accurate

etiergjt 24Mar2011 15:16

Re: PHP - geoIP country redirect
 
Quote:

Originally Posted by webdevel (Post 81007)
It's free. What you want to download is GeoLite Country. It's 99.5% accurate

oh,I see
I didn't notice the free/opensource link
Thanks

pidge1992 2Aug2011 22:28

Re: PHP - geoIP country redirect
 
but that code is not working for me :(

anyone can help ?


All times are GMT +5.5. The time now is 05:35.