Get User current location by IP address – Simple PHP Script!

This script will give you the User’s City, State and Country, Latitude, Langitude etc.,

To get the IP Address is easy in PHP right?, but to get the Country and City we need to use some third party API’s

Here is the one, which is free, go to the below link register and get a api key to :

http://ipinfodb.com/ip_location_api.php

 


http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=117.10.168.1&format=json

Do you know how to find the current IP address of the User?. If you don’t know, please see the below line

<?php

echo $_SERVER['REMOTE_ADDR'];

?>

Here is the entire code.


<?php
$url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=".$_SERVER['REMOTE_ADDR']."&format=json"));

//print the array to see the fields if you wish.
//print_r($url);

echo "<table border='1' width='50%' align='center'><tr><td>COUNTRY:</td><td>";
echo $url->countryName;
echo "</td></tr><tr><td>CITY:</td><td>";
echo $url->cityName;
echo "</td></tr><tr><td>STATE OR REGION:</td><td>";
echo $url->regionName;
echo "</td></tr><tr><td>IP ADDRESS:</td><td>";
echo $url->ipAddress;
echo "</td></tr><tr><td>COUNTRY CODE:</td><td>";
echo $url->countryCode;
echo "</td></tr><tr><td>LATITUTE:</td><td>";
echo $url->latitude;
echo "</td></tr><tr><td>LONGITUDE:</td><td>";
echo $url->longitude;
echo "</td></tr><tr><td>TIMEZONE:</td><td>";
echo $url->timeZone;
echo "</td></tr><tr></table>";

?>

This above script will give you the current location such as city, state, country code, latitude, longitude etc.,

You can also see the demo or download from below:

Demo Download

17 Comments

Add a Comment

Your email address will not be published. Required fields are marked *