PHP: 3 Ways to Get City/Country from IP Address

Updated: February 19, 2024 By: Guest Contributor Post a comment

Overview

Discovering the geographical location of a user based on their IP address can be very useful for various web applications. Be it for localizing content, implementing geo-restrictions, or simply gathering analytics, PHP offers several methods to achieve this. In this article, we’ll explore different solutions for getting the city or country from an IP address using PHP, discuss their implementations, and compare their pros and cons.

Solution 1: Using the GeoIP Extension

The GeoIP extension allows you to find the location of an IP address. This method requires installing the GeoIP PHP extension and possibly the GeoIP database.

  • Step 1: Install the GeoIP extension and the GeoIP database.
  • Step 2: Use the geoip_record_by_name() function to get location information.

Example:

$ip = '8.8.8.8';
$record = geoip_record_by_name($ip);
if ($record) {
    echo 'City: ' . $record['city'] . "\n";
    echo 'Country: ' . $record['country_name'] . "\n";
}

Notes: While the GeoIP extension is powerful, it requires additional setup and the data accuracy depends on the database’s freshness. It can be a fast solution once set up.

Solution 2: Using a Third-Party API Service

Several web services provide IP location services. For this method, you need to sign up for an API and use it in your PHP code. Popular services include IPInfo, IPStack, and GeoIP2 by MaxMind.

  • Step 1: Sign up for an API service and get an API key.
  • Step 2: Use PHP’s file_get_contents() or curl to perform an API request.
  • Step 3: Decode the JSON response to get the city/country information.

Example:

$apiKey = 'YOUR_API_KEY';
$ip = '8.8.8.8';
$url = "http://api.ipstack.com/$ip?access_key=$apiKey";
$response = file_get_contents($url);
$location = json_decode($response, true);
if ($location) {
    echo 'City: ' . $location['city'] . "\n";
    echo 'Country: ' . $location['country_name'] . "\n";
}

Notes: Using a third-party API is easy to implement and does not require maintaining a database. However, it may come with costs, rate limitations, and it relies on external service availability.

Solution 3: PHP’s Built-in GeoIP Functions (As of PHP 8)

PHP 8 introduced native support for GeoIP functionalities, allowing for a more streamlined approach without needing external extensions.

  • Step 1: Ensure your PHP installation is version 8 or higher and has GeoIP support enabled.
  • Step 2: Use the geoip_country_name_by_name() function for country information or look for similar functions for city data.

Example:

$ip = '8.8.8.8';
$country = geoip_country_name_by_name($ip);
if ($country) {
    echo 'Country: ' . $country . "\n";
}

Notes: This method offers a native, potentially more performant solution, but the actual functionality and data availability may be limited based on PHP’s implementation and the system’s GeoIP library.

Conclusion

In conclusion, obtaining a user’s geographical location from their IP address in PHP can be done through various means, each with its own benefits and drawbacks. Whether opting for the GeoIP extension, using a third-party API, or utilizing PHP’s built-in functions, you should consider factors such as implementation complexity, data accuracy, and potential costs. By carefully reviewing the available options, you can select the solution that best fits your application’s needs.