Does anyone know of a good Geo Redirect script?

Status
Not open for further replies.

zayd

Boom
May 29, 2007
276
2
0
There's some traffic I'm getting on one of my sites that I can't really do much with and I'd like to redirect it to another site instead of wasting it. Are there any reliable scripts that would be able to do this for me?
 


There is a free Maxmind database you can download and use. I'll post up some code for you when I get a chance. You can do what you want in less than a dozen lines.
 
OK, first, go download the free Maxmind database and the associated geoip.inc file. Then just use the following code:

Code:
include('geoip.inc'); // include the geoip functions
$geofile = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$cn= geoip_country_name_by_addr($geofile, $_SERVER['REMOTE_ADDR']);

switch ($cn) {
case "United Kingdom": {
header("Location: http://www.wickedfire.com/"); /* Redirect to whatever */
break;
}
case "United States": {
header("Location: http://www.blah.com/"); /* Redirect to whatever */
break;
}
default: {
// Put whatever you want it to do for all other locations here if you want
break;
}
}

geoip_close($geofile);
 
Thanks for the advice. Unfortunately, I'm not very experienced with programming, so this all looks a little complicated. I might have to hire someone to do it for me.
 
zayd, it's not actually that bad, the real magic happens on this line:
PHP:
$cn= geoip_country_name_by_addr($geofile, $_SERVER['REMOTE_ADDR']);
Bascially, that line starts off with declaring a variable to store the country corresponding with the IP. The junk after the equal sign is a function that takes the database file and the ip address and works its
magic by returning the country that corresponds with the IP Addy.

PHP:
switch ($cn) {
case "United Kingdom": {
header("Location: http://www.wickedfire.com/"); /* Redirect to whatever */
break;
}
This is where more magic happens, basically, once you have the country stored in the variable $cn it's a matter of acting on what that country actually is the switch statement basically compares the variable inside the () to the various case states and if they match it executes what's between the {}, i.e. in the above example, if $cn contains United Kingdom then it will preform a php redirect to the right website.

So all and all, the only thing you really have to do to add more countries is make sure the name of the country you want is in the database and then you add another case statement i.e.

PHP:
case "Mexico": {
  header("Location: http://www.mexico-travel.com/"); /* Redirect */
  break;
}
Hopefully it makes a little more sense, if you have more php related questions, feel free to ask here, or pm me or whatever.
 
Hey, thanks for explaining it, I really appreciate it. What I was worried about was the database though, I've never played with one before. And the geoip.inc file... I don't even know what that is and I can't find it on the site. I'm so lost, lol.
 
Yeah you don't even need to use the database in a database server. You can pull it out of a data file.
 
It was that GeoIP script I remembered about earlier. So I can vote for that script, it has been working for me.
 
Status
Not open for further replies.