Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to redirect domain according to country IP address in PHP?
You can redirect users to different domains based on their country IP address using the GeoIP extension or third-party services like geoPlugin. This technique is useful for creating localized websites or region-specific content.
Using geoPlugin Class
First, download the geoPlugin class from the official website ?
Installation: Download geoPlugin class from https://www.geoplugin.com/_media/webservices/geoplugin.class.phps and save it as
geoplugin.class.phpin your project directory.
Here's a complete example that detects the user's country and redirects accordingly ?
<?php
require_once('geoplugin.class.php');
// Create geoPlugin instance
$geoplugin = new geoPlugin();
$geoplugin->locate();
// Get country code from user's IP
$country_code = $geoplugin->countryCode;
// Redirect based on country code
switch ($country_code) {
case "AL": // Albania
header('Location: http://sq.wikipedia.org/');
break;
case "NL": // Netherlands
header('Location: http://nl.wikipedia.org/');
break;
case "FR": // France
header('Location: http://fr.wikipedia.org/');
break;
case "DE": // Germany
header('Location: http://de.wikipedia.org/');
break;
default: // All other countries
header('Location: http://en.wikipedia.org/');
break;
}
exit(); // Stop script execution after redirect
?>
Alternative Method with Array Mapping
For better maintainability, you can use an array to map country codes to domains ?
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;
// Define country-to-domain mapping
$country_domains = [
'AL' => 'http://sq.wikipedia.org/',
'NL' => 'http://nl.wikipedia.org/',
'FR' => 'http://fr.wikipedia.org/',
'DE' => 'http://de.wikipedia.org/',
'ES' => 'http://es.wikipedia.org/',
];
// Get redirect URL or default
$redirect_url = $country_domains[$country_code] ?? 'http://en.wikipedia.org/';
// Perform redirect
header('Location: ' . $redirect_url);
exit();
?>
How It Works
The geoPlugin class analyzes the visitor's IP address and determines their geographical location. The locate() method fetches location data, and countryCode property returns the ISO 3166 two-letter country code. The header() function then redirects the user to the appropriate domain.
Note: You can find the complete list of ISO 3166 country codes at https://www.geoplugin.com/iso3166
Key Points
- Always call
exit()afterheader()redirect to prevent further script execution - GeoIP detection may not be 100% accurate, especially for VPN users
- Consider caching geolocation results to improve performance
- Test with different IP addresses to ensure proper functionality
Conclusion
Country-based redirection using geoPlugin provides an effective way to serve localized content. The array mapping approach offers better code maintainability when handling multiple countries and domains.
