Get user's current location
193,655
Solution 1
<?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"];
?>
Solution 2
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'];
?>
Solution 3
<?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!
Solution 4
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);
Solution 5
MaxMind GeoIP is a good service. They also have a free city-level lookup service.
Author by
Jacek Francuz
The head of MilestonesIT - a software company, PHP, JS, SQL developer, Agile/Kanban enthusiast.
Updated on July 09, 2022Comments
-
Jacek Francuz almost 2 years
How can I determine user's current location based on IP (I guess it works this way).
-
Underdog over 9 yearsinstead of exploding the return you can use the json as return. api.hostip.info/get_json.php
-
Underdog over 9 years$json_string = 'api.hostip.info/…; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata, true);
-
Admin about 7 yearsGeoplugin 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)
-
Jay Blanchard about 7 yearsWhile 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.
-
Jay Blanchard about 7 yearsWhile 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.
-
Jay Blanchard about 7 yearsDo 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.
-
Jay Blanchard about 7 yearsWhile 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.
-
Jay Blanchard about 7 yearsWhile this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
-
Michael Rogers almost 7 yearsUseless with those limits.
-
Rohlik over 6 yearsThis site is not available anymore.
-
Vince over 6 yearsIs there a workaround for localhost servers? My ip address keeps coming up as ::1
-
Naveen DA over 6 years@vince Localhost always return ::1 ip address
-
Moses Schwartz almost 6 yearsdeprecation_message: "This API endpoint is deprecated and will stop working on July 1st, 2018. For more information please visit: github.com/apilayer/freegeoip#readme",
-
user2060451 about 5 yearsThis is a paid service.
-
lalithkumar about 5 yearsThe question is not about free service
-
klediooo almost 5 yearsFor what is it necessary to check all the forwarded ip? I thought only check
if(isset(HTTP_X_FORWARDED_FOR))
is necessary. -
RiggsFolly over 3 yearsAnd in many countries this will get you the location of the ISP not the actual user
-
mindmischief almost 3 yearsthey have a free. limited tier
-
Hristo about 2 yearsFound a free API ipapi.co/$PublicIP/json , or also single fields can be queried much quicker ipapi.co/$PublicIP/languages ! Check the docs at ipapi.co/api/#location-of-a-specific-ip
-
Pisumathu about 2 yearsIs it a free API?