Download this: PHP GeoIPLocation Library - 230kb (automatically updated daily)
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";?>
OR Use this Function
function countryCityFromIP($ipAddr){//function to find country and city from IP address//Developed by Roshan Bhattarai [url]http://roshanbh.com.np[/url]//verify the IP address for theip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";$ipDetail=array(); //initialize a blank array//get the XML result from hostip.info$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);//get the city name inside the node <gml:name> and </gml:name>preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);//assing the city name to the array$ipDetail['city']=$match[2];//get the country name inside the node <countryName> and </countryName>preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);//assign the country name to the $ipDetail array$ipDetail['country']=$matches[1];//get the country name inside the node <countryName> and </countryName>preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);$ipDetail['country_code']=$cc_match[1]; //assing the country code to array//return the array containing city, country and country codereturn $ipDetail;}
Impressive posting, really liked reading it. I like your writing style, it’s quite unique. Thanks for sharing the information here.
ReplyDeletephp development melbourne | local seo melbourne