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 or 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, 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.

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');
        $ipaddress = 'UNKNOWN';
    return $ipaddress;

echo $get_client_ip;

How to get visitor Location in PHP (

Let's assume you have created your API key at

$PublicIP = get_client_ip();
$json  = file_get_contents('$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'];


  • Country Code: US
  • Country Name: United States
  • GeoID: 6252001
  • State: Virginia
  • State Code: VA
  • City: Ashburn
  • Postal: 20149
  • Latitude: 39.0481
  • Longitude: -77.4728
  • RequestTime: 1601199805
  • RequestedIP:

How to get visitor Location in PHP (

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

$PublicIP = get_client_ip();
$json  = file_get_contents('$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'];


  • 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: 23151