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.
<br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br /><html xmlns="http://www.w3.org/1999/xhtml"> <br /><head> <br /><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <br /><title>Ip Address Lookup</title> <br /></head> <br /> <br /><body> <br /><?php <br />function countryCityFromIP($ipAddr) <br />{ <br /> $ipDetail=array(); <br /> $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr); <br /> preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match); <br /> $ipDetail['city']=$match[2]; <br /> preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches); <br /> $ipDetail['country']=$matches[1]; <br /> preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match); <br /> $ipDetail['country_code']=$cc_match[1]; <br /> return $ipDetail; <br />} <br />if(isset($_GET['ip'])) { <br /> $IPDetail=countryCityFromIP($_GET['ip']); <br /> echo "<table align='center' width='50%'>\n"; <br /> echo "<tr><td>IP Address </td> <td>" . $_GET['ip'] . "</td></tr>\n"; <br /> echo "<tr><td>Country Name</td> <td>" . $IPDetail['country'] . "</td></tr>\n"; <br /> echo "<tr><td>City </td><td>" . $IPDetail['city'] . "</td></tr>\n"; <br /> echo "<tr><td>Country Code</td><td>" . $IPDetail['country_code'] . "</td></tr>\n"; <br /> echo "</table>"; <br />} <br />else { <br /> ?> <br /> <form method="get"> <br /> <table align="center" width="50%"> <br /> <tr><td>IPADDRESS</td><td><input type="text" name="ip" /></td></tr> <br /> <tr><td><input type="submit" value="lookup" /></td><td><input type="reset" value="clear" /></td></tr> <br /> </table> <br /> </form> <br /> <?php <br />} <br />?> <br /></body> <br /></html> <br />
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