Use this IP address lookup tool to find detailed information and geolocation of any IP address. Besides serving as a functional IP lookup tool, this page also provides the full source code that powers the tool. I think developers who are curious about how IP lookup works internally will find the source code very useful. To start, just enter an IP address.
Note: When this page was first loaded, my code detected the IP address of your device and automatically filled it in for you. But if you are trying to lookup a different IP address, just type it into the text box below.
What Does An IP Address Lookup Tool Do?
An IP address lookup tool performs a reverse DNS lookup to help you find the hostname of any IPv4 address along with ownership information and geolocation details. In more simpler terms, an IP lookup tool like this one quickly lets you know who is behind a particular IP address you may have seen in your server’s logs.
If you are a server administrator and you notice some random IP addresses (that you don’t recognize) poking around your firewall and trying to gain access to your network, you will immediately want to be alert since such activities are a clear red flag. But before you go on the offensive, you may want to first use this IP address lookup tool to find the location, owner, and other important information about the IP addresses in question.
Common Use Cases For IP Address Lookup
- “Catch” would-be network intruders: You just got a notification from your firewall about a denied connection from some remote IP address that you don’t recognize. It is very possible that you are the target of a hacking attempt. If you do nothing, the nefarious user, may just keep trying until they gain access to your system and carry out some malicious activities. To quickly figure out whether someone is really trying to hack into your network, run a quick IP lookup using the above tool.
- Troubleshoot slow networks: When users start complaining about the Internet being too slow within your network, you can use this tool for troubleshooting. You may find out for example that a user is streaming movies or downloading huge torrents. By identifying details about the source of traffic on your network, you’re one step closer to identifying the bottleneck.
- Find the source of an IP address that accessed your social media accounts: You don’t have to be a server/web admin to use this tool. If you are notified, for example, that the last activity on a personal account of yours (e.g a social media account) came from an unknown IP, you can use this tool to investigate and find out who might be responsible – their country, city, etc.
How Does This IP Lookup Tool Work?
From a user perspective, this tool is pretty easy to use. Just enter a valid IPv4 address and press the “Lookup IP Address” button (note: the button will remain disabled until a valid IP address is entered).
When the button is pressed, the tool uses two different IP lookup APIs behind the scenes to retrieve both detailed and summarized information about the specified IP address. Using two independent APIs like this was done for a few different reasons, but one obvious benefit of this approach is that the user can quickly compare the results of one API against the other. I think this way, it is easy to judge the accuracy of the results to some extent.
Detailed IP address information is retrieved from the API provided by ipstack while summarized IP information comes via API provided by ipinfo.
My usage of both APIs factors in their different pricing plans. While both APIs offer a free plan, you will need to sign up with ipstack and get an API key even if you only want to use their free plan. As for ipinfo, neither signup nor API key is required as long as your usage is below certain request limits.
After retrieving the latitude and longitude information associated with a given IP address from ipstack, this IP address lookup tool goes further to geolocate the physical location on Google Maps. Again, in order to use Google Maps, you will need to get a Maps API key from the Google Developers Console. You will find some extra information about this within the source code below.
The PHP Code Powering This Tool
<?php $IP = $_SERVER['REMOTE_ADDR']; $ip = htmlentities($_GET["wp-admin-ip"]); // You may want to use a parameter with a more friendly name than "wp-admin-ip" $hostname = gethostbyaddr($_GET['wp-admin-ip']); $location = json_decode(file_get_contents('http://api.ipstack.com/'.$ip.'?access_key=YOUR_IPSTACK_API_KEY&format=1')); $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json")); $address = ""; if(isset($_GET['wp-admin-ip'])) { $self_url = htmlspecialchars($_SERVER["REQUEST_URI"]); echo '<form method="get" action="' . $self_url . '#detailed_information">'; foreach($_GET as $name => $value) { $name = htmlspecialchars($name); $value = htmlspecialchars($value); if($name !== "wp-admin-ip") { echo '<input type="hidden" name="'. $name .'" value="'. $value .'">'; } } $address = $location->latitude . " " . $location->longitude; echo '<input autocomplete="off" type="text" name="wp-admin-ip" id="wp-admin-ip" maxlength="15" placeholder="Enter IP Address Here" style="width:100%; background:#EEF;" onkeyup="wp_admin_ip_success()" /><br/><br/> <input type="submit" class="button" value="Lookup IP Address" disabled id="wp-admin-ip-button" /> </form>'; echo "<br><h2><a name='detailed_information' style='text-decoration: none; pointer-events: none; color: #000000;'>Detailed IP Information</a> Via <a href='https://ipstack.com/' rel='noopener noreferrer' target='_blank'>ipstack's</a> API</h2>"; echo "<b>IP Address: </b>" .$location->ip; echo "<br><b>IP Type: </b>" .$location->type; echo "<br><b>Continent Name: </b>" .$location->continent_name; echo "<br><b>Country Code: </b>" .$location->country_code; echo "<br><b>Country Name: </b>" .$location->country_name; echo "<br><b>City: </b>" .$location->city; echo "<br><b>State/Region: </b>" .$location->region_name; echo "<br><b>Region Code: </b>" .$location->region_code; echo "<br><b>Zip/Postal Code: </b>" .$location->zip; echo "<br><b>Hostname: </b>" .$hostname; echo "<br><b>Organization: </b>" .$details->org; echo "<br><b>Latitude: </b>" .$location->latitude; echo "<br><b>Longitude: </b>" .$location->longitude; echo "<br><b>Your Browser User-Agent String: </b>" .$_SERVER['HTTP_USER_AGENT']; echo "<br><br><h2>Summarized IP Information Via The API From <a href='https://ipinfo.io/' rel='noopener noreferrer' target='_blank'>ipinfo.io</a></h2>"; echo "<b>IP Address: </b>" .$details->ip; echo "<br><b>Country Code: </b>" .$details->country; echo "<br><b>City: </b>" .$details->city; echo "<br><b>State/Region: </b>" .$details->region; echo "<br><b>Zip/Postal Code: </b>" .$details->postal; echo "<br><b>Hostname: </b>" .$details->hostname; echo "<br><b>Organization: </b>" .$details->org; echo "<br><b>Location (Latitude and Longitude): </b>" .$details->loc; } else { $self_url = htmlspecialchars($_SERVER["REQUEST_URI"]); print ('<form method="get" action="' . $self_url . '#detailed_information">'); foreach($_GET as $name => $value) { $name = htmlspecialchars($name); $value = htmlspecialchars($value); if($name !== "wp-admin-ip") { echo '<input type="hidden" name="'. $name .'" value="'. $value .'">'; } } print ('<input autocomplete="off" type="text" name="wp-admin-ip" id="wp-admin-ip" maxlength="15" onkeyup="wp_admin_ip_success()" placeholder="Enter IP Address Here" value="'.$IP.'" style="width:100%; background:#EEF;" /><br/><br/> <input type="submit" class="button" value="Lookup IP Address" disabled id="wp-admin-ip-button" /> </form>'); } ?> <script type="text/javascript"> function wp_admin_ip_success() { ipaddress = document.getElementById("wp-admin-ip").value; if(ipaddress.length === 0 || !ipaddress.trim() || !ValidateIPaddress(ipaddress.trim())) { document.getElementById('wp-admin-ip-button').disabled = true; } else { document.getElementById('wp-admin-ip-button').disabled = false; } } wp_admin_ip_success(); function ValidateIPaddress(ipaddress) { if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) { return (true) } return (false) } </script> <?php function geocode($address) { $address = urlencode($address); // Get a Google Maps key from the Google Developers Console // This is API key is not referrer restricted. It is meant for server-side requests. $url = "https://maps.google.com/maps/api/geocode/json?address={$address}&key=YOUR_GOOGLE_MAPS_API_KEY_ONE"; $resp_json = file_get_contents($url); $resp = json_decode($resp_json, true); if($resp['status']=='OK'){ $lati = $resp['results'][0]['geometry']['location']['lat']; $longi = $resp['results'][0]['geometry']['location']['lng']; $formatted_address = $resp['results'][0]['formatted_address']; if($lati && $longi && $formatted_address) { $data_arr = array(); array_push( $data_arr, $lati, $longi, $formatted_address ); return $data_arr; } else { return false; } } else { return false; } } ?> <?php if(preg_match('/\S/', $address)) { $data_arr = geocode($address); if($data_arr) { echo "<br><br><h2>Geolocation (Based On Data From ipstack)</h2>"; $latitude = $data_arr[0]; $longitude = $data_arr[1]; $formatted_address = $data_arr[2]; ?> <style> /* Always set the map height explicitly to define the size of the div element that contains the map. */ #map { width:100%; height:30em; } </style> <a name='map_element_or_error' style='text-decoration: none; pointer-events: none; color: #000000; font-size: 1px;'></a><div id="map">Loading map...</div> <!-- This second Google Maps API key used below is referrer restricted key. It is meant specifically for JavaScript browser requests. --> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY_TWO"></script> <script> function init_map() { var myOptions = { zoom: 14, center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map"), myOptions); marker = new google.maps.Marker({ map: map, position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>) }); infowindow = new google.maps.InfoWindow({ content: "<?php echo $formatted_address; ?>" }); google.maps.event.addListener(marker, "click", function () { infowindow.open(map, marker); }); infowindow.open(map, marker); } google.maps.event.addDomListener(window, 'load', init_map); </script> <?php } else { echo "<a name='map_element_or_error' style='text-decoration: none; pointer-events: none; color: #000000;'><span style='color: red;'>ERROR: No map found!</span></a>"; } } ?>
I hope you found this IP lookup tool and its associated source code useful. I didn’t go into detailed explanation of the source code since I think most people would probably be more interested in the working tool itself.
However, if you have any questions or anything to add, please reach me via the comments section. I am always happy to help with or discuss stuff like this.
Leave a Reply