Display a visitor’s geolocation information using API in PHP

Display a visitor's geolocation information using API in PHP

Geolocation is a great way to put relevant content to your web visitors and it is a technology that can be used in almost any application you can think of.

In this tutorial, we use the IP2Location.io IP Geolocation API to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our API.

To use the IP2Location.io API, you will need an API key. If you don’t have one, get it from the below link.

https://www.ip2location.io/pricing

Below are the sample codes written in PHP to query and display user’s geolocation information.

<?php
$apiKey = 'YOUR_API_KEY';
$urlTemplate = 'https://api.ip2location.io/?' . 'ip=%s&key=' . $apiKey . '&format=json';

function getIPAddress() {  
	//whether ip is from the share internet  
	if(isset($_SERVER['HTTP_CLIENT_IP'])) {  
		$ipAddress = $_SERVER['HTTP_CLIENT_IP'];  
		}  
	//whether ip is from the proxy  
	elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
		$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];  
	}  
	//whether ip is from the remote address  
	else{  
		$ipAddress = $_SERVER['REMOTE_ADDR'];  
	}  
	return $ipAddress;  
}  
$ipAddress = getIPAddress();

// replace the "%s" with real IP address
$urlToCall = sprintf( $urlTemplate, $ipAddress);

$rawJson = file_get_contents( $urlToCall );

$data = json_decode( $rawJson, true );

$error = '';

if (isset($data['city_name'])) {
  if ($data['city_name'] != '-') {
    echo '<script language="javascript">';
    echo 'alert("Welcome Visitors from ' . $data['city_name'] . '")';
    echo '</script>';
  }
  else {
    echo '<center>You are in local server!</center><br>';
    echo '<script language="javascript">';
    echo 'alert("You are in local server!")';
    echo '</script>';
  }
}
else {
  $error = 'IP Address parsing error!';
}
?>
<html>
<head>
<title>IP2Location.io IP data API</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top">
<div class="container">
<a class="navbar-brand" href="#">Display a visitor’s geolocation information</a>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-10 text-center">

<p class="text-left my-4" style="font-size:1.3rem;">Your IP address: <?php echo $ipAddress; ?></p>
<div class="table-responsive mb-5">
<table class="table table-bordered table-hover">

<?php
if ($error == '') {
  echo '<tr><th>ip</th><td> ' . (isset($data['ip']) ? $data['ip'] : '') . "</td></tr>";
  echo '<tr><th>country_code</th><td> ' . (isset($data['country_code']) ? $data['country_code'] : '') . "</td></tr>";
  echo '<tr><th>country_name</th><td> ' . (isset($data['country_name']) ? $data['country_name'] : '') . "</td></tr>";
  echo '<tr><th>region_name</th><td> ' . (isset($data['region_name']) ? $data['region_name'] : '') . "</td></tr>";
  echo '<tr><th>district</th><td> ' . (isset($data['district']) ? $data['district'] : '') . "</td></tr>";
  echo '<tr><th>city_name</th><td> ' . (isset($data['city_name']) ? $data['city_name'] : '') . "</td></tr>";
  echo '<tr><th>latitude</th><td> ' . (isset($data['latitude']) ? $data['latitude'] : '') . "</td></tr>";
  echo '<tr><th>longitude</th><td> ' . (isset($data['longitude']) ? $data['longitude'] : '') . "</td></tr>";
  echo '<tr><th>zip_code</th><td> ' . (isset($data['zip_code']) ? $data['zip_code'] : '') . "</td></tr>";
  echo '<tr><th>time_zone</th><td> ' . (isset($data['time_zone']) ? $data['time_zone'] : '') . "</td></tr>";
  echo '<tr><th>asn</th><td> ' . (isset($data['asn']) ? $data['asn'] : '') . "</td></tr>";
  echo '<tr><th>as</th><td> ' . (isset($data['as']) ? $data['as'] : '') . "</td></tr>";
  echo '<tr><th>isp</th><td> ' . (isset($data['isp']) ? $data['isp'] : '') . "</td></tr>";
  echo '<tr><th>domain</th><td> ' . (isset($data['domain']) ? $data['domain'] : '') . "</td></tr>";
  echo '<tr><th>net_speed</th><td> ' . (isset($data['net_speed']) ? $data['net_speed'] : '') . "</td></tr>";
  echo '<tr><th>idd_code</th><td>' . (isset($data['idd_code']) ? $data['idd_code'] : '') . "</td></tr>";
  echo '<tr><th>area_code</th><td> ' . (isset($data['area_code']) ? $data['area_code'] : '') . "</td></tr>";
  echo '<tr><th>weather_station_code</th><td> ' . (isset($data['weather_station_code']) ? $data['weather_station_code'] : '') . "</td></tr>";
  echo '<tr><th>weather_station_name</th><td> ' . (isset($data['weather_station_name']) ? $data['weather_station_name'] : '') . "</td></tr>";
  echo '<tr><th>mcc</th><td> ' . (isset($data['mcc']) ? $data['mcc'] : '') . "</td></tr>";
  echo '<tr><th>mnc</th><td> ' . (isset($data['mnc']) ? $data['mnc'] : '') . "</td></tr>";
  echo '<tr><th>mobile_brand</th><td> ' . (isset($data['mobile_brand']) ? $data['mobile_brand'] : '') . "</td></tr>";
  echo '<tr><th>elevation</th><td> ' . (isset($data['elevation']) ? $data['elevation'] : '') . "</td></tr>";
  echo '<tr><th>usage_type</th><td> ' . (isset($data['usage_type']) ? $data['usage_type'] : '') . "</td></tr>";
  echo '<tr><th>address_type</th><td> ' . (isset($data['address_type']) ? $data['address_type'] : '') . "</td></tr>";
  echo '<tr><th>ads_category</th><td> ' . (isset($data['ads_category']) ? $data['ads_category'] : '') . "</td></tr>";
  echo '<tr><th>ads_category_name: </th><td>' . (isset($data['ads_category_name']) ? $data['ads_category_name'] : '') . "</td></tr>";
  echo '<tr><th>is_proxy</th><td> ' . (isset($data['is_proxy']) ? ($data['is_proxy'] ? 'True' : 'False') : '') . "</td></tr>";
  
  if (isset($data['continent'])) {
    $continent = $data['continent'];
    echo '<tr><th>continent => name</th><td> ' . $continent['name'] . "</td></tr>";
    echo '<tr><th>continent => code</th><td> ' . $continent['code'] . "</td></tr>";
    echo '<tr><th>continent => name</th><td> ' . $continent['name'] . "</td></tr>";
  }
  
  if (isset($data['country'])) {
    $country = $data['country'];
    echo '<tr><th>country => name</th><td> ' . $country['name'] . "</td></tr>";
    echo '<tr><th>country => alpha3_code</th><td> ' . $country['alpha3_code'] . "</td></tr>";
    echo '<tr><th>country => numeric_code</th><td> ' . $country['numeric_code'] . "</td></tr>";
    echo '<tr><th>country => demonym</th><td> ' . $country['demonym'] . "</td></tr>";
    echo '<tr><th>country => flag</th><td> ' . $country['flag'] . "</td></tr>";
    echo '<tr><th>country => capital</th><td> ' . $country['capital'] . "</td></tr>";
    echo '<tr><th>country => total_area</th><td> ' . $country['total_area'] . "</td></tr>";
    echo '<tr><th>country => population</th><td> ' . $country['population'] . "</td></tr>";
    echo '<tr><th>country => tld</th><td> ' . $country['tld'] . "</td></tr>";
    
    $currency = $country['currency'];
    echo '<tr><th>country => currency => code </th><td>' . $currency['code'] . "</td></tr>";
    echo '<tr><th>country => currency => name</th><td> ' . $currency['name'] . "</td></tr>";
    echo '<tr><th>country => currency => symbol</th><td> ' . $currency['symbol'] . "</td></tr>";
    
    $language = $country['language'];
    echo '<tr><th>country => language => code</th><td> ' . $language['code'] . "</td></tr>";
    echo '<tr><th>country => language => name</th><td> ' . $language['name'] . "</td></tr>";
  }
  
  if (isset($data['region'])) {
    $region = $data['region'];
    echo '<tr><th>region => name</th><td> ' . $region['name'] . "</td></tr>";
    echo '<tr><th>region => code</th><td> ' . $region['code'] . "</td></tr>";
  }
  
  if (isset($data['city'])) {
    $city = $data['city'];
    echo '<tr><th>city => name</th><td> ' . $city['name'] . "</td></tr>";
  }
  
  if (isset($data['time_zone_info'])) {
    $time_zone_info = $data['time_zone_info'];
    echo '<tr><th>time_zone_info => olson</th><td> ' . $time_zone_info['olson'] . "</td></tr>";
    echo '<tr><th>time_zone_info => current_time</th><td> ' . $time_zone_info['current_time'] . "</td></tr>";
    echo '<tr><th>time_zone_info => gmt_offset</th><td> ' . $time_zone_info['gmt_offset'] . "</td></tr>";
    echo '<tr><th>time_zone_info => is_dst</th><td> ' . ($time_zone_info['is_dst'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>time_zone_info => sunrise</th><td> ' . $time_zone_info['sunrise'] . "</td></tr>";
    echo '<tr><th>time_zone_info => sunset</th><td> ' . $time_zone_info['sunset'] . "</td></tr>";
  }
  
  if (isset($data['geotargeting'])) {
    $geotargeting = $data['geotargeting'];
    echo '<tr><th>geotargeting => metro</th><td> ' . $geotargeting['metro'] . "</td></tr>";
  }
  
  if (isset($data['proxy'])) {
    $proxy = $data['proxy'];
    echo '<tr><th>proxy => last_seen</th><td> ' . $proxy['last_seen'] . "</td></tr>";
    echo '<tr><th>proxy => proxy_type</th><td> ' . $proxy['proxy_type'] . "</td></tr>";
    echo '<tr><th>proxy => threat</th><td> ' . $proxy['threat'] . "</td></tr>";
    echo '<tr><th>proxy => provider</th><td> ' . $proxy['provider'] . "</td></tr>";
    echo '<tr><th>proxy => is_vpn</th><td> ' . ($proxy['is_vpn'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_tor</th><td> ' . ($proxy['is_tor'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_data_center</th><td> ' . ($proxy['is_data_center'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_public_proxy</th><td> ' . ($proxy['is_public_proxy'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_web_proxy</th><td> ' . ($proxy['is_web_proxy'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_web_crawler</th><td> ' . ($proxy['is_web_crawler'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_residential_proxy</th><td> ' . ($proxy['is_residential_proxy'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_consumer_privacy_network</th><td> ' . ($proxy['is_consumer_privacy_network'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_enterprise_private_network</th><td> ' . ($proxy['is_enterprise_private_network'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_spammer</th><td> ' . ($proxy['is_spammer'] ? 'True' : 'False' ) . "</td></tr>";
    echo '<tr><th>proxy => is_scanner </th><td>' . ($proxy['is_scanner'] ? 'True' : 'False') . "</td></tr>";
    echo '<tr><th>proxy => is_botnet</th><td> ' . ($proxy['is_botnet'] ? 'True' : 'False') . "</td></tr>";
  }
}
else {
  echo '<div class="alert alert-danger text-left">'.$error . "</div>";
}
?>
</table>
</div>
</div>
</div>
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

Output

Pop Out Box

Pop out box of visitor's location

Visitor’s Geolocation Information

Visitor's geolocation information

Video


THE POWER OF IP GEOLOCATION

Find a solution that fits.


Was this article helpful?

Related Articles