projects > php geoiplocation library - ip address to country
ARCHIVE: PHP GeoIPLocation Library
Please note that the library was last updated Sep 30, 2020 and the original source of geolocation data is no longer available.
If you find a resource that allows copyright free download of IP address database, please let me know.
If you find a resource that allows copyright free download of IP address database, please let me know.
- 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.
Live Demo of PHP GeoIPLocation Library
- Your IP address is: 18.219.25.226
- Your Country is: United States
Download PHP GeoIPLocation Library
- Download this: PHP GeoIPLocation Library - 943kb (automatically updated daily) (The GeoIPLoc file is compressed using GZip. I recommend using 7-Zip to extract it.)
Credits
- PHP GeoIPLocation Library works by downloading the IP address database from: Software77.net (A Webnet77.com Company)
- The above database has its own licensing and copyright policy. For license and legal requirements, refer to the URL above.
License
- The PHP GeoIPLocation Library is released under the Creative Commons license.
- While I use code and algorithms from various sources, I always respect the code license and give credit where it's due. If you think that I am using your code/algorithm without the appropriate license, please let me know.
Usage
- Download PHP GeoIPLocation Library from the link above.
- Extract geoiploc.php to any folder on your server that is accessible by a web browser.
- Include the "geoiploc.php file and call getCountryFromIP() function with any IP address.
- 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";
?>
Documentation
- For help and documentation on how to use the PHP GeoIPLocation Library, view the sample code above. View the source-code of geoiploc.php file for more information.
- Due to time constraints, I cannot write detailed documentation for PHP GeoIPLocation Library. However, feel free to email me for further information, technical questions, ideas, improvements etc.
Disclaimer
- Developer: Chirag Mehta, 3 Dec. 2005
- This text, algorithm, and design is copyrighted to Chirag Mehta, 2005.
- For reproduction / copyright information contact me.