Saturday, 21 September 2013

PHP GeoIPLocation Library


  • PHP GeoIPLocation Library by Chirag Mehta is a single PHP file that will let you determine the country of location for any given IP address.
  • GeoIPLoc does not require any large database tables, flat text files, or any special configuration on your server. Using GeoIPLoc is as easy as calling the getCountryFromIP() function with any IP address of the form 'x.x.x.x' where 'x' is any integer between 0 and 255.
  • The library is automatically updated once a day by downloading the free IP address database from Software77.net. The auto-generation utility then sorts and compacts the data in the most efficient way to reduce the size of the resulting PHP file.


  • Additionally, the library uses a rudimentary caching algorithm that drastically speeds up batch querying of IP addresses, e.g. while parsing browser access logs.


  • Download this: PHP GeoIPLocation Library - 230kb (automatically updated daily)

    Usage

    1. Download PHP GeoIPLocation Library from the link above.
    2. Extract geoiploc.php to any folder on your server that is accessible by a web browser.
    3. Include the "geoiploc.php file and call getCountryFromIP() function with any IP address.
    4. View the sample code below for more details.

    Sample Code


    <?php

      
    include("geoiploc.php"); // Must include this

      // ip must be of the form "192.168.1.100"
      // you may load this from a database
      
    $ip $_SERVER["REMOTE_ADDR"];
      echo 
    "Your IP Address is: " $ip "<br />";

      echo 
    "Your Country is: ";
      
    // returns country code by default
      
    echo getCountryFromIP($ip);
      echo 
    "<br />\n";

      
    // optionally, you can specify the return type
      // type can be "code" (default), "abbr", "name"

      
    echo "Your Country Code is: ";
      echo 
    getCountryFromIP($ip"code");
      echo 
    "<br />\n";

      
    // print country abbreviation - case insensitive
      
    echo "Your Country Abbreviation is: ";
      echo 
    getCountryFromIP($ip"AbBr");
      echo 
    "<br />\n";

      
    // full name of country - spaces are trimmed
      
    echo "Your Country Name is: ";
      echo 
    getCountryFromIP($ip" NamE ");
      echo 
    "<br />\n";?>



    OR Use this Function


    1. function countryCityFromIP($ipAddr)
    2. {
    3. //function to find country and city from IP address
    4. //Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url]
    5. //verify the IP address for the
    6. ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
    7. $ipDetail=array(); //initialize a blank array
    8. //get the XML result from hostip.info
    9. $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
    10. //get the city name inside the node <gml:name> and </gml:name>
    11. preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
    12. //assing the city name to the array
    13. $ipDetail['city']=$match[2];
    14. //get the country name inside the node <countryName> and </countryName>
    15. preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
    16. //assign the country name to the $ipDetail array
    17. $ipDetail['country']=$matches[1];
    18. //get the country name inside the node <countryName> and </countryName>
    19. preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
    20. $ipDetail['country_code']=$cc_match[1]; //assing the country code to array
    21. //return the array containing city, country and country code
    22. return $ipDetail;
    23. }

    1 comment:

    1. Impressive posting, really liked reading it. I like your writing style, it’s quite unique. Thanks for sharing the information here.
      php development melbourne | local seo melbourne

      ReplyDelete