PHP:How to lookup country code by GeoIP

    This document will show you how to use the GeoIP data easily and quickly to lookup country code.

    Download GeoIP package

    The Code

    First, include the library and open the geoip database:

    $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

    Then, getting the country code:

    $country_code = geoip_country_code_by_addr($gi,$ip);
    echo "Your country code is: $country_code \n";

    finally, close the geoip database:


    This solution uses a very fast and compact binary file as the database to lookup the country code by the IP address.

    Related articles

    comments powered by Disqus