GeoIP Country Location – PHP Script
Basically, this PHP script utilizes MaxMind‘s free database for determining the country of an IP address.
Download:
Step 1) Get country-redirect.php
Step 2) Get the most recent IP-country database.
Step 3) Extract, then upload both files to the same directory.
Demo: You may see where it thinks you are from.
Once you download the script, go to the end of country-redirect.php to customize its output (here’s an example of how to make it redirect).
More information
I’m not offering support for this script. The PHP coding wasn’t written entirely by me. I essentially combined a bunch of classes from MaxMind to make this work. I do not guarantee its functionality on your server. It should work for PHP4-5 at least.
The actual determining of the country is about 99.5% accurate. All AOL users are assumed to be from the United States.
In my case, I find this is helpful for affiliate offers that redirect based on a user’s country. This way (if you are an affiliate), you can select to which offers you’d like to send your international visitors.
Anyway, this should be helpful for providing more relevant content to all of your users.




19 Responses to “GeoIP Country Location – PHP Script”
I’s love to see more stuff written about showing the city also.. There’s a few free scripts that show city also..
@craig m: Yeah, very true. I was keeping this script for a single purpose, though.
hy i install but it showes me blank page
its working now.i just upload GeoIP.dat file as binary mode. Thanks for this
sorry, how redirect to a page?
@sentih: Here’s detailed instructions on how PHP redirects work: http://us2.php.net/manual/en/function.header.php
Can this be made to only look at the United States and geocode the States like Colorado, Florida, Etc. and send the users to a sub domain name if they in Colorado they would be sent to http://co.mydomain.com/
Hi, i installed but a blank page is being shown.
Hi Mike,
I got a blank page as wel. My $_SERVER['REMOTE_ADDR'] variable was a local/network address (192.168. etc). So it could not resolve it’s country, duh! Be sure to run the script with a public address or simply upload the script to a webserver so it receives your public ip-address.
Floris
I would just like to mention that although I give you kudos for effort, your code is very sloppy and no commenting or documentation. Very poor implementation of your work sir.
@Dan: If you read under “more information” you would have noticed that I wrote that I hardly programmed any of this. It’s a compilation of classes. If you’d like to improve the code and documentation for this free script, go ahead. The other users would likely appreciate it.
@Sam: It is possible. Creating automatic subdomains would be quite difficult though.
Hey Mike,
Thanks for your script. It’s working like a charm!
I would like to redirect only UK and US visitors to different pages. The rest of them should continue with the current page.
What exactly should I add in the code?
Cheers,
Seb
Go to line 503 of “country-redirect.php” and replace “echo $country;” with:
if($country == 'UK'){
header('Location: http://example.co.uk');
exit();
}
else if($country == 'US')
{
header('Location: http://example.com');
exit();
}
That should work – although I haven’t used this script in quite a while.
it’s not as good as 12script’s Power Redirector, but free, thanks
Thank, It wokrs great!
“Seb”, look for the line #368
1) return $gi->GEOIP_COUNTRY_NAMES[$country_id]; //if you want the contry’s name
2) return $gi->GEOIP_COUNTRY_CODES[$country_id]; //if you want the contry’s code
If you have your script, just write:
include(“country-redirect.php”);
and the script will write the code or the name of the country, but if you want something else, just replace the line #503 (echo $country;) with something other.
If you have questions, find my e-mail on my site:
http://www.atanasov.co.cc/index.php
Can this script be shortened so as to block only 1 country. It is 25k in size, so this is why for 1 country it could be only a few kb.
One more thing. If a country is disallowed / redirected, can a specific IP be allowed from that country?
I would really appreciate if someone could help me with the code I need to put at line 503 for this.
Thanks!
If you will use this scripts for redirect users to other page of your site, think about that the Google BOTS are also “users” and also they will be redirected, and in that way, your site will not be indexed in Google and you will have got an unbelevable big “trafic” to your site, because they will index sites that not exacts!
If you have questions, find my e-mail on my site (english, french, bulgarian):
http://www.atanasov.co.cc/en/
http://www.atanasov.co.cc/fr/
http://www.atanasov.co.cc/
Leave a Reply