![]() |
Getting Visitor's Country with PHP using Geo IP
IntroductionSometimes you just need to know what country your site visitors are coming from - for example, if you're trying to implement geo-targeted advertising. This article will show you how. Sometimes you just need to know what country your site visitors are coming from - for example, if you're trying to implement geo-targeted advertising. That's where a tool like MaxMind's GeoIP comes in - it lets you easily extract geographic data from your visitor's IP address. MaxMind makes available both commercial and free databases; the commercial ones are extremely precise and can get as fine-grained as the user's city, while the free version can only identify the country of origin. We'll use the free version in this article. If you need more detailed information, such as the remote client's city and state of origin, you will need to purchase a more detailed database from MaxMind. Getting startedTo use it, you'll have to first download the GeoIP Free Country file and extract it into a directory in your Web server. Then you'll have to pick which language API to use with the database file. For simplicity, we're going to use the pure PHP version because it doesn't require any additional configuration or Apache modules. Remember to read the license terms before installing these on your Web site to ensure you are in compliance. The code in Listing A demonstrates the basics of using the module (geoip.inc) to access the GeoIP Free Country database (GeoIP.dat). The example assumes both the PHP include and the country database file are in the same directory as the PHP file itself. You'll have to change the paths as needed if this is not the case in your installation. Code Listing A Code: PHP
We then use the handle returned by the call to geoip_open() to obtain the two-letter country code and human-friendly name corresponding to the given IP address, via the geoip_country_code_by_addr() and geoip_country_code_by_name() functions, respectively. Both functions accept two arguments: the handle returned by geoip_open() and the IP address to resolve. Once the required information is obtained, we close the database file with a call to geoip_close(). Simple as that. |
Re: Getting Visitor's Country with PHP using Geo IP
Mary:
I have a question for you that is related in a way. I would like to make a link on our Company web site only available to US internet users and prevent foreign countries from being able to access or even see the link. Do you know of a way with the use of PHP script or another language script to limit access? We want US customers to be able to link to our US distributors after they search for certain parts. Our European customers use international agents for stock information. Your input is greatly appreciated. SnoopySLD |
Re: Getting Visitor's Country with PHP using Geo IP
Yes thats the best way of doing it.
|
Re: Getting Visitor's Country with PHP using Geo IP
You can use the GeoIP Apache API
Blocking unwanted countries The following Apache configuration directives uses GeoIP Country to block traffic from China and Russia: Code:
GeoIPEnable On |
Re: Getting Visitor's Country with PHP using Geo IP
Quote:
Can i have the step by step tutorial?? thanking in advance. |
Re: Getting Visitor's Country with PHP using Geo IP
Please read the article above, it got most of the instructions, give it a try and let us know in case you face any problems!
|
Re: Getting Visitor's Country with PHP using Geo IP
Seems to be nice. but a stupid question i have. Which webhost will allow to upload a file of size more than 500kbs which supports PHP?
|
Re: Getting Visitor's Country with PHP using Geo IP
With a shared Webhost you might not be able to use the Apache API, you can go for the Database version of GeoIP!
|
Re: Getting Visitor's Country with PHP using Geo IP
Quote:
Sir ! Which kind of database it is using? From where I can get it? |
Re: Getting Visitor's Country with PHP using Geo IP
|
Re: Getting Visitor's Country with PHP using Geo IP
Sir, this site is providing 7mb CSV file to import into SQL database which is again more than 500KBs. So problem remains the same.Web-Host will not allow to upload file size more than 500KBs.
|
Re: Getting Visitor's Country with PHP using Geo IP
Quote:
1. Switch the webhost 2. Manually get that csv file into small small csv files and upload into the database. |
Re: Getting Visitor's Country with PHP using Geo IP
hello, I apologize for posting after a year of inactivity in this thread but I really need some help.
In the past 4 days I've been looking for a solution to filter traffic on my website acording to my users country. I saw this post and it seams perfect for my problem but sincerely I don't know where to start from. I follow the steps: 1. "To use it, you'll have to first download the GeoIP Free Country file and extract it into a directory in your Web server." - what should I download? I've downloaded a csv file and a geoip.dat file but I can't see any geoip.inc .... All I want is to allow onlu spanish IPs in my spain.php file ... Is that possible? Thank you |
Re: Getting Visitor's Country with PHP using Geo IP
Yes, that's possible, I guess you'll need to use the FilesMatch directive.
|
Re: Getting Visitor's Country with PHP using Geo IP
pradeep, I'll pay you 30$-$40 if you can do it for me because I have no idea
I have a shared hosting and my intention is to block all traffic except spain for spain.php and the same with france.php, us.php, etc. I would like (in the end) only to add some text/code in pages like spain.php so that I can do it myself when adding new pages (new countries) Can you do the job ? |
Re: Getting Visitor's Country with PHP using Geo IP
Yes I can do it, do you have GeoIP installed??
|
Re: Getting Visitor's Country with PHP using Geo IP
Try this free PHP class (importer for the csv file included) for GeoLite country database:
http://phpweby.com/software/ip2country |
Re: Getting Visitor's Country with PHP using Geo IP
By Philip Miseldine:-While the Internet is a global phenomenon that connects different people in different countries, many sites fail to target their content or functionality to visitors who speak languages other than English, or who live outside countries with the largest Internet user bases, like America. But, with nations like China using the Internet more and more, English-only is no longer a smart decision.
It's time to get smart. If you think that providing your site in another language will be a huge hassle, think again. GeoIP API, combined with your .NET Web applications, can create pages whose content is specifically tailored to the user on the basis of their geographical location -- this article will show how it's done. By the time we've finished, you'll be able to use GeoIP API and .NET to increase visitor satisfaction and, potentially, ad revenues for your site. GeoIP:-GeoIP is a product from MaxMind that maps given IP addresses to specific locations stored in a database of IP addresses. Different editions of GeoIP are available, but you can use the free edition of the country database to get 93% accurate mappings within your applications. Do check the licensing information to make sure your particular app is suitable for the free use option. MaxMind also provide a C# API, which we'll use in this article. You need to download both the API and the free country database to be able to use the code in this article: * Download the API here * . Download the country database here. |
Re: Getting Visitor's Country with PHP using Geo IP
So Nice ...
|
Re: Getting Visitor's Country with PHP using Geo IP
But,,, i can't download the CSV file yet ... :(
|
Re: Getting Visitor's Country with PHP using Geo IP
Please Help me ... Can u send another link 4 me, 2 download the CSV file
|
Re: Getting Visitor's Country with PHP using Geo IP
Quote:
|
Re: Getting Visitor's Country with PHP using Geo IP
Sometimes you just need to know what country your site visitors are coming from—for example, if you're trying to implement geo-targeted advertising. That's where a tool like MaxMind's GeoIP comes in—it lets you easily extract geographic data from your visitor's IP address.
MaxMind makes available both commercial and free databases; the commercial ones are extremely precise and can get as fine-grained as the user's city, while the free version can only identify the country of origin. We'll use the free version in this article. If you need more detailed information, such as the remote client's city and state of origin, you will need to purchase a more detailed database from MaxMind. |
Re: Getting Visitor's Country with PHP using Geo IP
and can somebody give a code which allows to redirect users to special pages depending on their country [for example peopl from usa/uk/ca to english.htm, from france,belgium to french.html, rest to rest.html] ?
can it be done with php similar to what was shown here? |
| All times are GMT +5.5. The time now is 06:49. |