IP lookup using PHP | Plot location on Google Map using IP Address
anoopsachari | Sep 18, 2010 | Comments 8
Many of you would have seen IP lookup pages and that would give you details of your IP Address , your location etc. I planned to implement the same using PHP and was able to implement successfully . I am sharing it with you , hope this would help you in your projects .
I have plotted your location too on a Google Map . If you are using this code in any of your project please do register a Google Map API key from here and replace the ” APIKEY ” with the API key you get.
The code gives you following output .
The package contains only a single file named index.php .
contents of index.php
<!--
PHP IP Lookup.
Author : Anoop S Achari
My web : http://www.achari.in
-->
<?php
$ip = '';
if(isset($_POST['ipSubmit']))
{
$ip = $_POST['ipAddress'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$xml = simplexml_load_file('http://ipinfodb.com/ip_query.php?ip='.$ip.'&timezone=true');
$ip = $xml->Ip;
$status = $xml->Status;
$country = $xml->CountryName;
$region = $xml->RegionName;
$city = $xml->City;
$latitude = $xml->Latitude;
$longitude = $xml->Longitude;
$timezone = $xml->TimezoneName;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://maps.google.com/maps?file=api&v=1&key=APIKEY" type="text/javascript"></script>
</head>
<body>
<div align="center">
<div align="center" style="width:470px;padding-top:20px;">
<form action="" method="post">
<label>Submit your IP :</label><input type="text" style="margin-left:10px;" value="<?php echo $ip; ?>" name="ipAddress" id="ipAddress"/>
<input type="submit" value="lookup" name="ipSubmit" />
</form>
<table width="50%" border="0" cellspacing="0" cellpadding="0" style="margin-top:10px;">
<tr>
<td width="58%" style="padding-right:10px;"><div align="right">Your Ip Address :</div></td>
<td width="42%"><?php echo $ip; ?></td>
</tr>
<tr>
<td style="padding-right:10px;"><div align="right">Country :</div></td>
<td><?php echo $country; ?></td>
</tr>
<tr>
<td style="padding-right:10px;"><div align="right">Region :</div></td>
<td><?php echo $region; ?></td>
</tr>
<tr>
<td style="padding-right:10px;"><div align="right">City :</div></td>
<td><?php echo $city; ?></td>
</tr>
<tr>
<td style="padding-right:10px;"><div align="right">Timezone :</div></td>
<td><?php echo $timezone; ?></td>
</tr>
</table>
</div>
<div id="map" style="width: 400px; height: 300px; margin-top:20px;"></div>
<script type="text/javascript">
var map = new GMap(document.getElementById("map"));
var point = new GPoint(<?php echo $longitude; ?>,<?php echo $latitude; ?>);
map.centerAndZoom(point, 3);
var marker = new GMarker(point);
map.addOverlay(marker);
</script>
</div>
</body>
</html>
Note : The Map would give you an approximate location .
Filed Under: javascript • php
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.

connect me on






Thanx..Great job anoop
@ratheesh thankyou friend.
Hey Buddy,
I am trying your code but it point out wrong place & this does not generate state & city name.
Please Help me Buddy.
Thanks,
So, since IPInfoDB now require an API, I altered the script. There is an updated version here:
http://www.rogerethomas.com/b/40/php-plot-users-ip-on-google-maps
@anoopsachari – Great job on this script, feel free to take the one from my site and put it up on here. It’s super helpful!
@roger thomas – thank you for your info. i would put your code in my blog .
@anoopsachari – feel free to use it. You did an outstanding job on the initial version!
he Dear Sir
i m beginner in php, and got yor website good . it has some nice tutorial like 1 gogole map IP . i m trying to add in my assignment. bt not showing map except just point
pleas elet me know what problem
thankc
waiting to hear from u
regards
shahabz ahmed
Hey very cool site!! Man .. Excellent .. Superb .. I’ll bookmark your website and take the feeds also?I am satisfied to search out a lot of useful info right here in the post, we’d like develop extra techniques on this regard, thanks for sharing. . . . . .