My thoughts and a repository for things I find useful

Web Developer @ Gumpo Ltd
Email wil2095@protonmail.com
X / Twitter @wil2095

Reverse IP lookup

PHP

Research

Development

As an extension to my 'referer' project I also capture the IP address of the client as they arrive at my site using

$_SERVER["REMOTE_ADDR"]

I thought it would be an interesting exercise to see if I could do a reverse lookup on the IP address to identify any A (address) records held in each case.
I found a useful and free service at ipwho.is.
Using PHP's cURL is was able to provide any IP address and return some cool data.


$id = $_POST["id"];
$ip = $_POST["ipaddress"];

$ch = curl_init('http://ipwho.is/'.$ip);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$ipwhois = json_decode(curl_exec($ch), true);
curl_close($ch);

$country = "";
$flag = "";
$city = "";
$domain = "";

if (isset($ipwhois['country']))
    $country = $ipwhois['country'];

if (isset($ipwhois['flag']['emoji']))
    $flag = $ipwhois['flag']['emoji'];

if (isset($ipwhois['city']))
    $city = $ipwhois['city'];

if (isset($ipwhois['connection']['domain']))
    $domain = $ipwhois['connection']['domain'];

echo "{ \"id\": ".$id.", \"city\": \"".$city."\", \"country\": \"".$country."\", \"flag\": \"".$flag."\", \"domain\": \"".$domain."\" }";     

This was all done using an AJAX call where I used jQuery's .each() function on an arrary of IP's. The printed value is specific to my code requirement. You can see the output by doing a default search on your own IP address: ipwho.is. Neat tool. You can use it for free but there is a limit to the amount of lookups you can do each month.