1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Getting Visitor's Country with PHP using Geo IP

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

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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
    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.
     
    shabbir likes this.
  2. snoopysld

    snoopysld New Member

    Joined:
    Mar 29, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Yes thats the best way of doing it.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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
    
     
  5. waikiat86

    waikiat86 New Member

    Joined:
    Jun 20, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    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

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:

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

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
  11. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    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

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    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

    Joined:
    Jun 13, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Yes, that's possible, I guess you'll need to use the FilesMatch directive.
     
  15. arround

    arround New Member

    Joined:
    Jun 13, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Yes I can do it, do you have GeoIP installed??
     
  17. Blagoj

    Blagoj New Member

    Joined:
    Oct 17, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
  18. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    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

    Joined:
    Jul 10, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Freelancer
    Home Page:
  20. rendra_mm2

    rendra_mm2 New Member

    Joined:
    Jul 10, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Freelancer
    Home Page:
    But,,, i can't download the CSV file yet ... :(
     

Share This Page