1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Getting Visitor's Country with PHP using Geo IP

Discussion in 'PHP' started by pradeep, Mar 21, 2007.

  1. pradeep

    pradeep Team Leader

    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

    // include functions

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

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

    // close database handler

    // 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.
    shabbir likes this.
  2. snoopysld

    snoopysld New Member

    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.
  3. pradeep

    pradeep Team Leader

    Yes thats the best way of doing it.
  4. pradeep

    pradeep Team Leader

    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:
    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
  5. waikiat86

    waikiat86 New Member

    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.
  6. pradeep

    pradeep Team Leader

    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!
  7. Bhullarz

    Bhullarz New Member

    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?
  8. pradeep

    pradeep Team Leader

    With a shared Webhost you might not be able to use the Apache API, you can go for the Database version of GeoIP!
  9. Bhullarz

    Bhullarz New Member

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

    pradeep Team Leader

  11. Bhullarz

    Bhullarz New Member

    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.
  12. shabbir

    shabbir Administrator Staff Member

    There are 2 options available to you.

    1. Switch the webhost
    2. Manually get that csv file into small small csv files and upload into the database.
  13. arround

    arround New Member

    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
  14. pradeep

    pradeep Team Leader

    Yes, that's possible, I guess you'll need to use the FilesMatch directive.
  15. arround

    arround New Member

    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 ?
  16. pradeep

    pradeep Team Leader

    Yes I can do it, do you have GeoIP installed??
  17. Blagoj

    Blagoj New Member

  18. gkumar

    gkumar New Member

    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.
  19. rendra_mm2

    rendra_mm2 New Member

    So Nice ...
  20. rendra_mm2

    rendra_mm2 New Member

    But,,, i can't download the CSV file yet ... :(

Share This Page