How can I determine a user's current location based on IP address (I guess it works this way).
7 Answers
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
?>
-
5Geoplugin is not best platform. It is limited to 120 actions per minute. It worked for me, but lately it started to show 403 for all requests from my server. Who knows why. (I haven't made those 120 connections in one minute)– user5147563Mar 30, 2017 at 17:18
-
4
Edited
Change freegeoip.net into ipinfo.io
<?php
function get_client_ip()
{
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} else if (isset($_SERVER['HTTP_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} else if (isset($_SERVER['REMOTE_ADDR'])) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}
return $ipaddress;
}
$PublicIP = get_client_ip();
$json = file_get_contents("http://ipinfo.io/$PublicIP/geo");
$json = json_decode($json, true);
$country = $json['country'];
$region = $json['region'];
$city = $json['city'];
?>
-
6While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Apr 24, 2017 at 19:31
-
Is there a workaround for localhost servers? My ip address keeps coming up as ::1– VinceDec 25, 2017 at 20:59
-
1
-
6deprecation_message: "This API endpoint is deprecated and will stop working on July 1st, 2018. For more information please visit: github.com/apilayer/freegeoip#readme", May 23, 2018 at 17:09
-
For what is it necessary to check all the forwarded ip? I thought only check
if(isset(HTTP_X_FORWARDED_FOR))
is necessary.– kledioooJul 18, 2019 at 17:32
<?php
$query = @unserialize (file_get_contents('http://ip-api.com/php/'));
if ($query && $query['status'] == 'success') {
echo 'Hey user from ' . $query['country'] . ', ' . $query['city'] . '!';
}
foreach ($query as $data) {
echo $data . "<br>";
}
?>
Try this code using this source. *It works!
-
8Do or do not. There is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. Apr 24, 2017 at 19:31
-
Try this code using the hostip.info service:
$country=file_get_contents('http://api.hostip.info/get_html.php?ip=');
echo $country;
// Reformat the data returned (Keep only country and country abbr.)
$only_country=explode (" ", $country);
echo "Country : ".$only_country[1]." ".substr($only_country[2],0,4);
-
1instead of exploding the return you can use the json as return. api.hostip.info/get_json.php– UnderdogOct 8, 2014 at 11:56
-
$json_string = 'api.hostip.info/…; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata, true);– UnderdogOct 8, 2014 at 12:02
-
As PHP relies on a server, the real-time location can’t be provided. Only a static location can be provided. It is better to avoid to rely on the JavaScript for a location rather than using PHP.
But there is a need to post the JavaScript data to PHP, so that it can easily be accessible to a program on the server.
An IP address gives you a quite unreliable location. You could Ajax the location upon load with JavaScript if it isn't critical to have the location at first. (Also, the user needs to give you its permission to access it.)
-
1While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Apr 24, 2017 at 19:31
The old freegeoip API is now deprecated and will be discontinued on 2018-07-01.
The new API is from ipstack. You have to create the account in ipstack. Then you can use the access key in the API URL.
$url = "http://api.ipstack.com/122.167.180.20?access_key=ACCESS_KEY&format=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$city = $response->city; // You can get all the details like longitude, latitude from the '$response'.
For more information, check at freegeoip (GitHub).
-
1
-
1
-