How to get the surfers country using PHP? Most coders need to tackle this problem sooner or later. There are several options out there. There is even a package called GeoIP Location. However, since this does not come with most default installations, it may not be your best choice.
A company called Maxmind specializes in professional GeoIP solutions. However, when you dig through their website, you can find that they also provide some free databases and free code. I will show you how to use this free stuff in a portable way to easily and quickly add an Ip to Country lookup feature to your PHP script in 2 easy steps.
Step 1. Download database and PHP file
- Download GeoLite Country (binary format), and extract the file GeoIP.dat.
- Download geoip.inc.
- Upload them to your server
Step 2. The Code
First, include the library and open the geoip database:
// include the php script
include("geoip.inc");
// open the geoip database
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
Getting the country code:
// to get country code $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country code is: $country_code \n";
Getting the country name:
// to get country name $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); echo "Your country name is: $country_name \n";
And finally, close the geoip database:
// close the database geoip_close($gi);
This solution uses a very fast and compact binary file as the database, and it does not add any to load to your MySQL server. You can do thousands of lookups per second on very average hardware with this code.
#1 by Derekp on September 24th, 2009
| Quote
I think i’ve seen this somewhere before…but it’s not bad at all
#2 by Ron on October 12th, 2009
| Quote
This is great! Thank you for that!
Just saw your entry @ Nettuts, I am starting with working with CodeIgniter too, but I don’t get Doctrine installed, could you send me the total files in a .zip file or so? As total including CodeIgniter (for example your final product at the screencast), that would be so great.
Thanks anyway for both tutorials! Hope to hear you!
#3 by Burak on October 15th, 2009
| Quote
Email sent.
#4 by Rahul on November 25th, 2009
| Quote
Nice.. Have been looking for this..
But is it possible to get the location(city)???
#5 by Burak on November 25th, 2009
| Quote
They do have a city database, but it’s not free.Nevermind, I just found they have a free version, that is a bit less accurate:
http://www.maxmind.com/app/geolitecity
#6 by Rahul on November 25th, 2009
| Quote
Oh Great!!!!!!!!!!!! Thanx dude..
#7 by Julian on January 8th, 2010
| Quote
Hi Burak, nice explanation.
Thanks.
Pingback: Using GeoIP in CodeIgniter application | jigniter™
#8 by Denis Molan on February 27th, 2010
| Quote
Hi,
Like the way of this code, short and quick but doesn’t IP change so binery file must be updated … where can i find this updated files ?
#9 by Mario on May 5th, 2010
| Quote
Hello, I can show the visitor’s country flag?
Thank