When developing custom website projects, it’s common to need the user’s country information for personalisation, analytics, or security reasons. For example, an eCommerce site might show prices in the local currency, or a streaming service might enforce geo-restrictions.
In this tutorial, we’ll use PHP and a free geolocation API to determine the country from an IP address. This method is lightweight, accurate, and compatible with both IPv4 and IPv6 addresses. Whether you’re part of a web development team or seeking to hire a PHP developer for your project, this step-by-step guide will help you incorporate IP-based geolocation into your website.
Why Get Country from IP Address?
Integrating IP-based location detection is beneficial in many web development services:
- Content personalization – Display language, currency, and offers based on the visitor’s country.
- Security – Block suspicious traffic from specific regions.
- Analytics – Track where your users are coming from.
- Compliance – Follow regional laws like GDPR.
Folder Structure
To keep the project clean, we’ll structure it as follows:
This modular approach is considered best practice in custom website development because it separates logic from presentation.
Step 1 – index.php: Creating the IP Form and Displaying Results
This file:
- Loads the Request class
- Accepts IP input from the user or uses the visitor’s IP
- Validates the IP address
- Retrieves and displays country information
Step 2 – Request.php: Handling IP Retrieval, Validation, and API Calls
In PHP web development services, separating business logic into individual classes enhances maintainability and scalability. Here’s the Request class:
How This Works
- Get the Client IP – The script checks multiple $_SERVER variables to detect the actual client IP, even if behind a proxy.
- Validate the IP – Ensures the IP is a public IPv4 or IPv6.
- Fetch Location Data – Uses ipwhois.app API via cURL.
- Display Results – Shows country name, country code, and IP.
Sample Output
Entering 8.8.8.8 would show:
- Country Name: United States
- Country Code: US
- IP Address: 8.8.8.8
Best Practices for IP Geolocation in PHP
- Cache results to avoid repeated API calls.
- Use a premium API key for higher accuracy and more data fields.
- Integrate this into your custom website development projects for tailored user experiences.
- If you hire a PHP developer, ensure they follow a modular approach for better maintainability.
Final Words
Finding the country from an IP address in PHP is straightforward and very useful for web development projects. By using a modular structure and a free API, you can quickly add geolocation to your applications. Whether you run a web development business or work with a dedicated team, this feature can boost personalisation, enhance security, and deliver better analytics.
If you’re looking to add features like this to your project, you can hire a PHP developer or collaborate with an experienced web development company to ensure robust and scalable solutions.
+91-8727000867
+64 22 003 5555



Request Instant Call
Hire Remotely