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.




15 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
Leave a Reply