IP Trace in PHP

Developers ONLY

As a quick snippet I was asked: how do you get useful information from an IP Address?

Once you have the variable which is the IP address assumed from some database or directly asĀ $_SERVER[‘REMOTE_ADDR’]

create some PHP f() along the lines of:

$ip = $_POST[‘ipAddress‘];

$locationDetails = json_decode(file_get_contents(“http://ipinfo.io/{$ip}/json”));

assuming the ipAddress is passed as a variable or in this case I’m assuming it’s an AJAX function hence the $_POST.

The second line is the important one which creates an object $locationDetails which you can use as:

$locationDetails->org;
$locationDetails->city;
$locationDetails->region;
$locationDetails->country;
$locationDetails->loc;

The last field in my list is the ->loc one, which is a comma delimited Latitude and Longitude which you can parse with:

$coOrdinates = explode(“,”, $locationDetails->loc);
$Lat = $coOrdinates[0];
$Long = $coOrdinates[1];

Hope this saves you some time searching for a solution. As for getting the real address [postal] from an IP address; that’s another kettle of fish entirely and you’ll need to employ the services of some agencies for that. You can’t go beyond the ISP, if you get my meaning.

Henry

Leave a reply:

Your email address will not be published.

Site Footer