How to get Location form IP Address in PHP« Back to Questions List

How to get Location from IP address using PHP
Posted by
Asked on September 8, 2016 9:33 am

// Get the longitude and latitude data using IP Address of user.

$user_data = json_decode(file_get_contents(''.$_SERVER['REMOTE_ADDR']));
$latitude = $user_data->latitude;
$longitude = $user_data->longitude;

// Use the longitude and latitude from above to get the area name of user location.

$json = file_get_contents(''.$latitude.','.$longitude.'&sensor=true');
$data = json_decode($json, true);

$area_name =  $data['results'][1]['address_components'][0]['long_name'];
echo $area_name;

Posted by
Answered On September 8, 2016 9:35 am