kode yard
all about C++ and PHP
Saturday, October 17, 2009
Country and City from ip address (php)
Here is a code, I made from scratch to get the geographical information from ip address.
Ip Address Lookup
function countryCityFromIP($ipAddr)
{
$ipDetail=array();
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
preg_match("@
(\s)*
(.*?)
@si",$xml,$match);
$ipDetail['city']=$match[2];
preg_match("@
(.*?)
@si",$xml,$matches);
$ipDetail['country']=$matches[1];
preg_match("@
(.*?)
@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1];
return $ipDetail;
}
if(isset($_GET['ip'])) {
$IPDetail=countryCityFromIP($_GET['ip']);
echo "
\n";
echo "
IP Address
" . $_GET['ip'] . "
\n";
echo "
Country Name
" . $IPDetail['country'] . "
\n";
echo "
City
" . $IPDetail['city'] . "
\n";
echo "
Country Code
" . $IPDetail['country_code'] . "
\n";
echo "
";
}
else {
?>
IPADDRESS
}
?>
This code does not implement any validation of address. As it depends upon whois server to perform lookup, the result takes time to show output.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment