Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Getting Visitor's Country with PHP using Geo IP (http://www.go4expert.com/articles/getting-visitors-country-php-using-geo-t3511/)

Mary 21Mar2007 11:14

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. 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 started



To 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

<?php

// include functions
include("geoip.inc");

// read GeoIP database
$handle = geoip_open("GeoIP.dat", GEOIP_STANDARD);

// map IP to country
echo "IP address 62.149.130.132 located in " . geoip_country_name_by_addr($handle, "62.149.130.132") . " (country code " . geoip_country_code_by_addr($handle, "62.149.130.132") . ")";

// close database handler
geoip_close($handle);

// print compulsory license notice
echo "<p> -- This product includes GeoIP data created by MaxMind, available from http://maxmind.com/ --";

?>

The sample code is pretty straightforward. After including the GeoIP PHP function library, the first step is to open the GeoIP database file with the geoip_open() function. This function accepts two arguments: the path to the database file and the type of database.

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.

snoopysld 30Mar2007 02:02

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

Mary 30Mar2007 07:46

Re: Getting Visitor's Country with PHP using Geo IP
 
Yes thats the best way of doing it.

pradeep 30Mar2007 11:02

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
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here

Deny from env=BlockCountry

# Optional - use if you want to allow a specific IP address from the country you denied
# (See http://httpd.apache.org/docs/1.3/mod/mod_access.html for more details)
Allow from 10.1.2.3


waikiat86 14Jul2007 21:31

Re: Getting Visitor's Country with PHP using Geo IP
 
Quote:

Originally Posted by pradeep
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
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here

Deny from env=BlockCountry

# Optional - use if you want to allow a specific IP address from the country you denied
# (See http://httpd.apache.org/docs/1.3/mod/mod_access.html for more details)
Allow from 10.1.2.3


Hello, I am so in need of this. Where should I put this code into. If i want to block all the IPs from USA which country code should i use?
Can i have the step by step tutorial?? thanking in advance.

pradeep 16Jul2007 10:11

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!

Bhullarz 24Sep2007 23:42

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?

pradeep 25Sep2007 10:10

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!

Bhullarz 26Sep2007 16:20

Re: Getting Visitor's Country with PHP using Geo IP
 
Quote:

Originally Posted by pradeep
With a shared Webhost you might not be able to use the Apache API, you can go for the Database version of GeoIP!


Sir ! Which kind of database it is using? From where I can get it?

pradeep 27Sep2007 11:12

Re: Getting Visitor's Country with PHP using Geo IP
 
www.maxmind.com


All times are GMT +5.5. The time now is 09:28.