RPeraltaJr
6/26/2017 - 2:33 PM

Find user's location based on IP Address

Find user's location based on IP Address

<?php 

// API (10,000 Requests Per Month) - https://ipstack.com/
$api_key = ""; // can be obtained through https://ipstack.com/quickstart
$request_url = "http://api.ipstack.com/{$_SERVER['REMOTE_ADDR']}?access_key={$api_key}&format=1";
$location_data = file_get_contents($request_url);
echo $location_data;

// API (1,000 Requests Per Day) - http://ipinfo.io/
$ip = $_SERVER['REMOTE_ADDR'];
$data = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $data->city;

// API (Free Service?) - http://www.geoplugin.net/
$ip = $_SERVER['REMOTE_ADDR']; 
$dataArray = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
echo $dataArray->geoplugin_regionCode;