The Internet is like a street lined with shops and businesses. But unlike brick-and-mortar shops with a fixed physical presence on a local street, the ones on the Internet crop up on a virtual broadway, globally connecting all the countries worldwide.
That's why any serious online business would want to track the visitor's location from where visitors are landing on its website. Fortunately, this is easily achieved with an IP address. This article will guide you through several possibilities for extracting country and related information from the IP address by leveraging a free geolocation API with PHP.
Getting Country Name from IPv4 IP Address
IPv4 is still the dominant format of IP addressing used across the world. Therefore, most visitor's IP address logs on web servers will still have this familiar format, with four numbers separated by three dots.
Let's write a simple PHP code to parse an IP v4 address to extract the country name. For this to work, you must have a PHP development environment with the latest PHP8 version. Additionally, you should also subscribe to Abstract API IP Geolocation API.
Don't reinvent the wheel. Abstract's APIs are production-ready now.
Abstract's suite of API's are built to save you time. You don't need to be an expert in email validation, IP geolocation, etc. Just focus on writing code that's actually valuable for your app or business, and we'll handle the rest.
This API works for multiple IP addresses. So instead of probing each visitor's IP address with a separate API call, you may want to generate a report of all visitor countries from your web server IP data logs.
You can achieve a report format per your needs by passing multiple IP addresses to the API request. For example, here is how you can do it for a CSV formatted report of IP addresses to country names.
As you can see, the output of this PHP script is a CSV formatted report of visitor IP address mapped to country and continent names.
Based on your requirements, you can modify the report format to deliver an abridged record with country code , region code, and continent code, along with IP data.
Can You Find Country Name from IP Address?
You can find the country name from an IP address, provided the IP address is public and globally addressable. Apart from that, you can also find the city and region names of the IP address within the country. Moreover, with the help of Abstract IP Geolocation API, it is possible to get these details along with timezone, ISP, and location coordinate specific IP address information.
How to find the Country of IP Address?
There is no direct way of decoding the country name from the IP address. But, it is possible to access a database that tracks IP address ranges allocation details across all ISPs operating in various geographies. However, with the help of Abstract IP Geolocation API, it is possible to get this information instantly. IP geolocation API maintains long-standing relationships with ISPs and other data providers to provide highly accurate and consistent country-specific data for IP addresses. Along with the country name, the API also offers valuable information about the location of the IP address, such as region, city, and location coordinates.
Why is IP Address Mapped to Country Name?
IP addresses are allocated to various Internet Service Providers (ISP) in different regions worldwide. All these ISPs operate as autonomous systems, which form the basis for the global routing of Internet traffic to and from all continents. IANA is the body that maintains a set of registries of IP addresses. Both IPv4 and IPv6 addresses are generally assigned hierarchically. ISPs assign users IP addresses. ISPs obtain allocations of IP addresses from a local Internet registry (LIR) or National Internet Registry (NIR), or their appropriate Regional Internet Registry (RIR). Thus all the users are mapped to one of the LIR or NIRs mapped to a country. This arrangement allows for tracking user IP addresses based on country names. If you want complete location details about all the information associated with IP addresses, you can use the Abstract's free Geolocation API.
Abstract's IP Geolocation API comes with libraries, code snippets, guides, and more.