How does Programmer Lookup Geolocation Information using IP Address and IP2Location?

This PHP tutorial helps programmers to find geolocation of IP address. There is a lot of third party API and libraries available in PHP, but here I will discuss about steps to find location, country, state, city, coordinates etc using free IP2Location PHP library.

What is IP2Location?

IP2Location helps to find IP Geo Location such as country, state, city, ISP, net speed, domain, connection type, postal code, area code, weather station, mobile carrier information using a single IP address query.

IP2Location offers you a great variety choices of IP location lookup solutions in various technologies, platforms, operating systems and programming libraries.

IP2Location LITE edition is an open-source version of database with limited accuracy and number of records access. You can use it free for personnel but for commercial not.

What is the solutions using IP2Location?

There are many solutions you can use with IP2Location such as:

  • Display native language and currency.
  • Redirect web pages based on geographical.
  • Digital Rights Management.
  • Prevent password sharing and abuse of service.
  • Reduce credit card fraud.
  • Web log statistics and analysis.
  • Auto-selection of country on forms.
  • Geo targeting for increased sales and clickthrough.
  • Spam filtering by location.

How to Integrate IP2Location with PHP?

We will use IP2Location PHP library to find GEO location of IP. Developers can use the API to query all IP2Location binary databases for IPv4 and IPv6 address.

You can install IP2Location using composer or download PHP Library file.

Extract the IP Location PHP class file and database folder from ZIP file. We have created index.php file and included IP2Location.php into index.php file. The sample script of IP2Location are as below:

Create database instance and fetch all information against IP address.

Where '8.8.8.8' is the target IP address for lookup GEO Information.

You can cache the database into memory to accelerate lookup speed but you must have enough RAM in your server.

$db = new \IP2Location\Database('./databases/IP-COUNTRY-SAMPLE.BIN', \IP2Location\Database::MEMORY_CACHE);

We will use table to display information,

How do I use IP2Location RESTful API?

You can also use IP2Location REST API of to retrieve IP information without installation of database. IP2Location is providing REST API service and you will receive a search result via API. Search results are derived from IP2Location database index of over 4 billion unique IP addresses. The IP2Location REST API supports IPv4 and IPv6.

All successful HTTP or HTTPS request will return a response code of 200. In our REST API, it also returns the lookup country code if successful or error code if failed. You can test this REST API as easy as using a web browser.

Sample codes of IP2Location API:

The response would be in json format:

In summary, you can perform geolocation using the free IP2Location LITE and PHP Library. It is very easy to implement and have been serving the industry since 2002.