GeoIP Redirect Script /w Maxmind Free Database (non-JS version)

mointernet

Regular Member
Joined
Apr 21, 2008
Messages
442
Reaction score
213
This is the non-javascript version of the GeoIP redirect script and I take no credit for the codes below as I believed similar script were posted very long ago but you know peeps here do not search that far back so I hope this will help some people here. I personally run thousands of traffic daily thru this script using media buys with no problems, the only maintenance you need is to manually update the Maxmind database file monthly and of course edit the country codes to your traffic requirements.

The advantage of using this here other than for the purpose of redirecting traffic according to country codes, this script uses php codes (non JS) and read its database through a flat file database (no mySQL) available for free and you can update it monthly (Maxmind website). Not only its fast and its pretty accurate.


copy & save (edit and add country codes where necessary) this code as a geoip.php file. note: The default line should be there all times as it redirect all else traffic
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");

// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code)

    {
        case "US": header("Location: http://yourUSAofferURL.com"); break;
        case "CA": header("Location: http://yourCAofferURL.com"); break;
        case "GB": header("Location: http://yourUKofferURL.com"); break;
        default: header("Location: http://yourINTLofferURL.com");
}
?>


download & unzip the GeoIP.dat file from the URL (you should manually update this monthly for accuracy).
Code:
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/


unzip geoip.zip (attachment) and place geoip.inc, GeoIP.dat and geoip.php together and send your traffic through the geoip.php file. **You may rename geoip.php to whatever filename you like.
 

Attachments

Thanks mate for the php code. I already use MaxMind in Delphi, and it's good to have it for PHP too!
 
Is it possible to run the script from inside, my index page?

I tried with a script like your and I did not manage to make it work, so I used the javascript script.

Would be better to advert your domain, has
Code:
www.domain.com
than
Code:
www.domain.com/whatever.php

At least for my need.

Thanks
 
Is it possible to run the script from inside, my index page?

I tried with a script like your and I did not manage to make it work, so I used the javascript script.

Would be better to advert your domain, has
Code:
www.domain.com
than
Code:
www.domain.com/whatever.php

At least for my need.

Thanks

save the php code to index.php and place geoip.inc and GeoIP.dat in your root directory. You can redirect your traffic through www.domain.com without typing index.php or whatever.php.
 
How do i add it to wordpress?

this is a standalone script so it might need some mods to make it work with wordpress. It will be better to find a Geo plugin for WP if there is any out there.
 
Thanks a lot I am going to implement straight away.


UPDATE: work perfect.

Thanks.
 
Last edited:
I was looking for it the whole of yest and now i see you posted it here.lol thanks and rep.it works great.
 
thanks so much for this. been trying to filter out all these pesky indian traffic from my pages.
 
man this script is one of the most usefull script ive found here. Very nice job!

I was looking for it the whole of yest and now i see you posted it here.lol thanks and rep.it works great.

same for me man lol i was looking at that geoip.zip and was trying to understand how the hell that thing was working there something missing... than after 30min i find out this thread with all explaination!! haha
 
Last edited by a moderator:
Is it possible to run the script from inside, my index page?

I tried with a script like your and I did not manage to make it work, so I used the javascript script.

Would be better to advert your domain, has
Code:
www.domain.com
than
Code:
www.domain.com/whatever.php

At least for my need.

Thanks

just name the php file index.php?
 
PHP:
<?php

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

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

// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code)

    {
        case "US": header("Location: xxx"); break;
        case "CA": header("Location: xxxxx"); break;
        case "GB": header("Location: xxxx"); break;
}
?>

I removed the default line so if someone not from those 3 country he can see the current page but if someone from those 3 country he goes to my site from those country.
 
I removed the default line so if someone not from those 3 country he can see the current page but if someone from those 3 country he goes to my site from those country.

that is if you have integrated the Geo php code with the rest of your html/php page, it will show the content of the page if they are not from those 3 countries.

But for standalone script from my original post, you have to leave the default line intact to divert the rest of the traffic somewhere else because the php code do not have any html/php page for visitors to see.
 
For UK traffic: The country code in geoip.inc is actually GB.

(In the switch-case instead of UK, put GB)
 
Whats the speed of the remote calls to MaxMind like? We're currently using a local DB in MySQL but it has a couple of IP address ranges missing or incorrect.

-aReJay
 
Let's say you wanted to put the .php script on every page of your site and then send all visitors from... say China...to ****** or any other URL.

Would we need to place the .inc and .dat file in a different location or will things still work as you have them laid out?
 
@realidan dude, i think you can directly use your ****** link in the single php file. just specify which location will you want your link to go live.

@OP

You just made my day! :-) This is the best script sooooooooooooooooo far! Thanks a lot!
 
Back
Top