url redirection based on Location

sapo

Power Member
Joined
Feb 25, 2008
Messages
510
Reaction score
275
I need a redirection script that will redirect all USA traffic to one page and every other to another. let me know. I need this ASAP so whoever can help Gets rep whenever I see your post lol
 
Download the appropriate files for php here: maxmind.com

This should do the trick ;)

Code:
<?php

include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code) {
  case "US": header("Location: http://US-URL"); break;
  default: header("Location: http://RestOfTheWorld-URL");
}
?>
 
Thanks But I couldent figure it out, Just gives me a blank page no redirection happens
 
Are the geoip.dat and geoip.inc files both inside the same folder as the page?
 
Are the geoip.dat and geoip.inc files both inside the same folder as the page?

I might sound dumb but I couldent find no geoip.dat

I have the geoip.inc but no .dat

LOL I am a idiot I just had to scroll down and read some more, damn their font too small thats my excuse lol
 
now both the files are in the same folder but still no redirection just blank
 
Could you paste the entire code you have here? :)

its the same as that person provided

Code:
<?php

include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code) {
  case "US": header("Location: http://yahoo.com"); break;
  default: header("Location: http://google.com");
}
?>

and i have geoio.inc and GeoIP.dat in the same directory as this index.html file
 
The code works great, just retested on one of my servers OP.

1. Delete the 2 files geoip.inc and GeoIP.dat you have now (maybe tainted?)

2. Make sure you get the geoip.inc from here:

http://geolite.maxmind.com/download/geoip/api/php/geoip.inc

3. get geoip.dat.gz (that you'll decompress so you'll end up with GeoIP.dat) from here:

http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Retry and it'll work for sure.

HTH! :)

its the same as that person provided

Code:
<?php

include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code) {
  case "US": header("Location: http://yahoo.com"); break;
  default: header("Location: http://google.com");
}
?>

and i have geoio.inc and GeoIP.dat in the same directory as this index.html file
 
The code works great, just retested on one of my servers OP.

1. Delete the 2 files geoip.inc and GeoIP.dat you have now (maybe tainted?)

2. Make sure you get the geoip.inc from here:

http://geolite.maxmind.com/download/geoip/api/php/geoip.inc

3. get geoip.dat.gz (that you'll decompress so you'll end up with GeoIP.dat) from here:

http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

Retry and it'll work for sure.

HTH! :)

Did everything you said and still just blank and when i check the source the code is their but it just doesnt trigger a redirection.
 
Are you using this on an html page? You shouldn't be able to see any source code when you load the page in your browser from your web server. Your page must have a .php file extension and you should be inserting that code before the opening <html> tag. You must test this on a web server - just opening the page in your browser from your local hard drive won't parse the php code.

If you want to parse .html files for php includes, you can set that up in your .htaccess file (google "parse php in html htaccess").
 
Are you using this on an html page? You shouldn't be able to see any source code when you load the page in your browser from your web server. Your page must have a .php file extension and you should be inserting that code before the opening <html> tag. You must test this on a web server - just opening the page in your browser from your local hard drive won't parse the php code.

If you want to parse .html files for php includes, you can set that up in your .htaccess file (google "parse php in html htaccess").

LMFAO soon as i read that I faced Palmed, Its works man, Thankfs for clearing that up.. Thanks evefyoen for their help
 
Checklist:

1. You put the above code into a file with a php extension, right (not htm/html)?
2. You are testing this with a hosting that actually supports php (you never know...)?
3. To do some infamous "printf debugging" change this part 'header("Location: http://yahoo.com");' with 'echo "I am a US visitor";' and see if it shows up.

If this is not the case it eludes me how such a simple snippet wouldn't work for ya.
 
Back
Top