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.

Share:
193,655
Jacek Francuz
Author by

Jacek Francuz

The head of MilestonesIT - a software company, PHP, JS, SQL developer, Agile/Kanban enthusiast.

Updated on July 09, 2022

Comments

  • Jacek Francuz
    Jacek Francuz almost 2 years

    How can I determine user's current location based on IP (I guess it works this way).

  • Underdog
    Underdog over 9 years
    instead of exploding the return you can use the json as return. api.hostip.info/get_json.php
  • Underdog
    Underdog over 9 years
    $json_string = 'api.hostip.info/…; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata, true);
  • Admin
    Admin about 7 years
    Geoplugin 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
    Jay Blanchard about 7 years
    While 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
    Jay Blanchard about 7 years
    While 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
    Jay Blanchard about 7 years
    Do 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
    Jay Blanchard about 7 years
    While 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
    Jay Blanchard about 7 years
    While 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
    Michael Rogers almost 7 years
    Useless with those limits.
  • Rohlik
    Rohlik over 6 years
    This site is not available anymore.
  • Vince
    Vince over 6 years
    Is there a workaround for localhost servers? My ip address keeps coming up as ::1
  • Naveen DA
    Naveen DA over 6 years
    @vince Localhost always return ::1 ip address
  • Moses Schwartz
    Moses Schwartz almost 6 years
    deprecation_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
    user2060451 about 5 years
    This is a paid service.
  • lalithkumar
    lalithkumar about 5 years
    The question is not about free service
  • klediooo
    klediooo almost 5 years
    For what is it necessary to check all the forwarded ip? I thought only check if(isset(HTTP_X_FORWARDED_FOR)) is necessary.
  • RiggsFolly
    RiggsFolly over 3 years
    And in many countries this will get you the location of the ISP not the actual user
  • mindmischief
    mindmischief almost 3 years
    they have a free. limited tier
  • Hristo
    Hristo about 2 years
    Found 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
    Pisumathu about 2 years
    Is it a free API?