Get Location From IP Address PHP Script

Last Updated:

If you want to track your visitor's location details like city, state, country, etc then you can use the following script. In PHP we can easily get the visitors/ user IP address by using $_SERVER['REMOTE_ADDR'] option. But you can't get other information like country, city, state, etc directly in PHP.

To get User's location detail form IP address we need to take help from 3rd party websites like www.snoopi.io or www.ipstack.com. Both the website provides free API Key to get user location details. But in the free version, you can send maximum 10,000 requests.

In the case of www.snoopi.io, you can send 1 request per 2 seconds. if you have very low traffic on your website and don't need a paying subscription.

How to get user IP Address in PHP

Let's create a simple PHP function to get the user IP address.

<?php
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}

echo $get_client_ip;
?>

How to get visitor Location in PHP (www.snoopi.io)

Let's assume you have created your API key at www.snoopi.io.

<?php
$PublicIP = get_client_ip();
$json  = file_get_contents('https://api.snoopi.io/$PublicIP?apikey=[YOUR API KEY]');
$json  =  json_decode($json ,true);
?>

The above code provides the JSON output for the following CountryCode, CountryName, GeoID, State, StateCode, City, Postal, Latitude, Longitude, RequestTime, RequestedIP


$CountryCode  = $json['CountryCode'];
$CountryName  = $json['CountryName'];
$GeoID        = $json['GeoID'];
$State        = $json['State'];
$StateCode    = $json['StateCode'];
$City         = $json['City'];
$Postal       = $json['Postal'];
$Latitude     = $json['Latitude'];
$Longitude    = $json['Longitude'];
$RequestTime  = $json['RequestTime'];
$RequestedIP  = $json['RequestedIP'];

Output

  • Country Code: US
  • Country Name: United States
  • GeoID: 6252001
  • State: Virginia
  • State Code: VA
  • City: Ashburn
  • Postal: 20149
  • Latitude: 39.0469
  • Longitude: -77.4903
  • RequestTime: 1710836592
  • RequestedIP: 172.70.211.23

How to get visitor Location in PHP (www.ipstack.com)

In comparison to snoopi.io, ipstack.com provides more useful information but it has limitation in the free version. Before starting you need to create your own API key at https://ipstack.com/product. Once you have generated your API key you can use the same program as above.

<?php
$PublicIP = get_client_ip();
$json  = file_get_contents('http://api.ipstack.com/$PublicIP?access_key=[YOUR API KEY]');
$json  = json_decode($json ,true);
?>

Once you get all data in JSON format you can save it in your variable and use as required.
$continent_name = $json['continent_name'];
$country_code	= $json['country_code'];
$country_name   = $json['country_name'];
$region_code    = $json['region_code'];
$region_name    = $json['region_name'];
$city           = $json['city'];
$zip            = $json['zip'];
$latitude       = $json['latitude'];
$longitude      = $json['longitude'];

Output

  • Continent Name: North America
  • Country Code: US
  • Country Name: United States
  • State: Virginia
  • State Code: VA
  • City: Ashburn
  • Postal: 20147
  • Latitude: 39.043701171875
  • Longitude: -77.474197387695


Download File

Total Downloads: 23920
Top