Explanations can be reviewed in full at https://geoip-db.com/. In this portal described various script code to read a location via IP. The codes are:
JSON object - https://geoip-db.com/json
JSONP callback - https://geoip-db.com/jsonp
or https://geoip-db.com/jsonp/54.191.123.456
A jQuery example with html markup
A plain JSONP javascript example, no jQuery required.
PHP script
A C # .NET WPF script
These are the example PHP scripts of Geoip (Geolocation by IP Address):
<?php
$json = file_get_contents('https://geoip-db.com/json');
$data = json_decode($json);
print "<TABLE width=\"333\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
print "<TR align=\"center\">\n";
print "<TD>No.</TD>";
print "<TD align=\"center\">Description </TD>";
print "<TD align=\"center\">Your Location</TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>01</TD>";
print "<TD align=\"center\">Country Code</TD>";
print "<TD align=\"center\"><b>". $data->country_code . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>02</TD>";
print "<TD align=\"center\">Country Name</TD>";
print "<TD align=\"center\"><b>". $data->country_name . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>03</TD>";
print "<TD align=\"center\">State/Province</TD>";
print "<TD align=\"center\"><b>". $data->state . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>04</TD>";
print "<TD align=\"center\">City</TD>";
print "<TD align=\"center\"><b>". $data->city . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>05</TD>";
print "<TD align=\"center\">Postal</TD>";
print "<TD align=\"center\"><b>". $data->postal . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>06</TD>";
print "<TD align=\"center\">Latitude</TD>";
print "<TD align=\"center\"><b>". $data->latitude . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>07</TD>";
print "<TD align=\"center\">Longitude</TD>";
print "<TD align=\"center\"><b>". $data->longitude . "</b></TD>";
print "</TR>\n";
print "<TR align=\"center\">\n";
print "<TD>08</TD>";
print "<TD align=\"center\">IPV4</TD>";
print "<TD align=\"center\"><b>". $data->IPv4 . "</b></TD>";
print "</TR>\n";
print "</TABLE>";
print "<br><br>";
print "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
print "<TR align=\"center\">\n";
print "<TD><a href=\"https://geoip-db.com/\">Source: https://geoip-db.com/</a></TD>";
print "</TR>\n";
?>
The result:
No. | Description | Your Location |
01 | Country Code | US |
02 | Country Name | United States |
03 | State/Province | Washington D.C. |
04 | City | Boardman |
05 | Postal | 97818 |
06 | Latitude | 45.6789 |
07 | Longitude | -123.456 |
08 | IPV4 | 54.191.123.456 |
Source: https://geoip-db.com/ |
If these scriptings are not working, please change your .htaccess on your hosting.
====================
/public_html/.htaccess
====================
# HTID:1234567: DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES BELOW
php_value display_errors 1
php_value allow_url_fopen 1
php_flag allow_url_fopen 1
php_value file_get_contents 1
# DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES ABOVE HTID:1234567:
<IfModule mod_php5.c>
php_value display_errors 1
php_flag register_globals off
php_value date.timezone "America/Washington"
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?(imagegambar.com)(/)?.*$ [NC]
RewriteRule ^(.*)$ https://geoip-db.com/ [R,NC]
Also change your /etc/php5/php.ini
======
php.ini
======
allow_url_fopen = On;
Attention Please!!! If you put on these scriptings on free-hosting and you can't change php.ini, and then automatically you can't be using the PHP scripting's. But you still using https://geoip-db.com/ for get the ip location with Javascript:
<html> <head> <title>GEOIP DB - jQuery example</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <div>Country: <span id="country"></span> <div>State: <span id="state"></span> <div>City: <span id="city"></span> <div>Latitude: <span id="latitude"></span> <div>Longitude: <span id="longitude"></span> <div>IP: <span id="IPv4"></span> <script> $.ajax({ url: "https://geoip-db.com/jsonp", jsonpCallback: "callback", dataType: "jsonp", success: function( location ) { $('#country').html(location.country_name); $('#state').html(location.state); $('#city').html(location.city); $('#latitude').html(location.latitude); $('#longitude').html(location.longitude); $('#ip').html(location.IPv4); } }); </script> </body> </html>
Source