Bayesean Blog - Desktop, Mobile and IOT Developer Blog

Create a Visual IP Address Geolocation with PHP

Posted on 21st May 2017 in Bootstrap 4, CSS


Creating a PHP PDO IP Address Geolocation Map


This is an update of the original Blog that used API a then free API.  As of July 2018 the old Freegeoip API was discontinued and a completely re-designed API has been provided at While the new API offers the same capabilities as the old one and also has the option of returning data in the legacy format, the API URL has now changed and all users are required to sign up for a free API Access Key to use the service. Fortunately the limit is 10000 calls per month. I have updated the code in the Github repository and I removed the Direct Viewer.

The purpose of the project is to update ip-addresses on to a World Map as shown in action using my own personal signed up API key.



  • PHP server side language use PHP 5.6 and higher.
  • JVector Maps. – Javascript based library .
    • jVectorMap uses only native browser technologies like JavaScript, CSS, HTML, SVG or VML. No Flash or any other proprietary browser plug-in is required. This allows jVectorMap to work in all modern mobile browsers.
  • Updated to Bootstrap 4 (framework).
  • Geolocation Library –
    • Head over to and follow the instructions to create your account and obtain your access token.
    • If you only need basic IP to Geolocation data and do not require more than 10,000 requests per month, you can use the free account. If you'd like more advanced features or more requests than included in the free account you will need to choose one of the paid options.
    • You can find an overview of all available plans at – (JavaScript Object Notation) is a lightweight data-interchange format. Designed to easily read and write. Quick to parse and generate.
    • While the old freegeoip API was limited to provide only the most basic IP to location data, our new API provides more than 20 additional data points including Language, Time Zone, Current Time, Currencies, Connection & ASN Information, and much more. To learn more about all the data points available
    • 10000 free calls per month.

Creating an IPStack account


Creating a free account is effortless and on signing up you are presented with a key and a lot of information on how to access the IPStack site. I have updated the SRC code and simply add your key in to the code where asked for and the project will run normally.


Geolocation Json Received Information

Calling the newer Server…

The old to the new increased available results...

Changed from




Updated to








Coding the Project

Geolocate.php (Updated)

This holds server side code that receives the IP Address in a ‘Post Request’ then calls the API rest-server and receives the JSON response.

The JSON is decoded to an Array and this then adjusted to fit the Jvector Map marker format. This JSON styled object is sent to the Browser.

if (!empty($_POST)){
   $ip =  trim($_POST['ip']);
   if (!filter_var($ip, FILTER_VALIDATE_IP)){
     $ip = " Is not a valid ip address";
{         //Set the path
$ipaddress ="".trim($_POST['ip'])."?access_key=yourfreeAPIkey";

//Reads the file at the path returns a string (no options used)

$ipaddress = file_get_contents($ipaddress);
//  Alert ip info
$info = $ipaddress;

// decode the jSON string received into an associative array in anPHP variable

$response = json_decode($ipaddress,true);

if (empty($response)){
$phpstr = "NO IP address sent by Host";
// Manipulate the array into a map marker JSON Object format
$new = "[{'latLng'  : [" .$response['latitude']. ", ".$response['longitude']. "] ,  'name':  '".$response["country_name"]."'}]";

//Send to  Javascript (client) as a JSON type Object which in PHP means an Array (No direct translation between PHP objects and JSON objects)
$phpstr = $new;
Adding the code for the JvectorMap - Javascript receives the PHP variable into a Javascript variable object which is then directly used by JvectorMap.



Feel free to download the Code from The Github Repository,


Download Code


Happy Coding

Reg      Commented   3 years ago Reply

I found in Stack Overflow that they use a foreach statement to pull the data. Your method is different. Which is the best method?

Admin      Replied    Moderator   3 years agoReply

Yes I am aware of the Stack Overflow method suggested. The problem is that we are dealing with server side and client side script. The foreach method updates the Client on every call. So if you has 500 rows of data, then you will be persistently updating the client 500 times and as it is a single threaded call, the client will simply stop working until the process is complete. Loading the data into Json in the Server creates a lightweight data object. This is only sent once to the client and the Json object (string) is quickly unpacked in the client allowing a quicker response. I hope that this clears up your concerns.

Add a Comment


Recent News

Delphi Delimited String to Fields
Delphi A Professional VCL DBGrid Part Four
Delphi A Professional VCL DBGrid Part Three
Delphi A Professional VCL DBGrid Part Two
Delphi A Professional VCL DBGrid Part One
Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction


Bootstrap 4
Delphi VCL
Delphi FMX
Bootstrap 3
XE4>Delphi > XE4
Delphi < XE4


August 2019

Delphi Delimited String to Fields

June 2019

Delphi A Professional VCL DBGrid Part Four

May 2019

Delphi A Professional VCL DBGrid Part Three

April 2019

Delphi A Professional VCL DBGrid Part Two

March 2019

Delphi A Professional VCL DBGrid Part One

November 2018

Delphi VCL Buttons in DBGrid

October 2018

Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button