Knowing how to detect visitors by country using PHP is very useful, as there are plenty of PHP development projects require country detection ability, such as:
- Fraud/spam prevention
- Country-based content distribution or redirection
This tutorial will guide you on how to utilize GeoIP to detect, distribute and block visitors by country using PHP and GeoIP.
Prerequisites
You must have PHP with GeoIP installed, in which GeoIP can be:
- PHP GeoIP PECL extension – Using legacy MaxMind GeoIP geolocation database
- PHP GeoIP2 library API – Using newest MaxMind GeoIP2 geolocation database
We already have an in-depth tutorial on how to install GeoIP and GeoIP2 extension with database files included:
How to install PHP GeoIP extension & GeoLite2 library (databases included)
After having GeoIP installed, we can start using it.
Detect visitors by country using PHP and GeoIP
As mentioned, there are two types of GeoIP extension/API, so there will be different sections for each of them.
1. PHP GeoIP PECL extension
To start using PHP GeoIP PECL extension, first, you have to specify the legacy GeoIP database file path:
geoip_setup_custom_directory('/usr/local/share/GeoIP.dat');
Then we can get the visitor’s 2-letter country code using the geoip_country_code_by_name
function with the user IP as the parameter:
$countryCode = geoip_country_code_by_name( $_SERVER['REMOTE_ADDR'] );
*You can check the country name and code list on the official IBAN website.
*User IP note: $_SERVER['REMOTE_ADDR']
will not give the correct user IP address if your website is running under a proxy, either DNS proxy or a WAF / Web Application Firewall (eg. CloudFlare, Imperva/Incapsula).
Quick solution:
-
- For CloudFlare, the user IP is stored in
$_SERVER['HTTP_CF_CONNECTING_IP']
variable - For Imperva/Incapsula, the user IP is stored in
$_SERVER['HTTP_INCAP_CLIENT_IP']
variable
- For CloudFlare, the user IP is stored in
The function will return the user’s country code based on their IP, for example, US
as United States, CA
as Canada.
Usage: After knowing the country code, it is easy to handle the tasks using PHP conditional. For example:
// Exit the page for user from United States
if ( $countryCode === 'US' ) {
exit();
}
// Exit the page for ALL users EXCEPT United Kingdom
if ( $countryCode !== 'GB' ) {
exit();
}
// Redirect Canada user to another website
if ( $countryCode === 'CA' ) {
header( 'Location: ' . $otherWebsite );
exit();
}
The conditional codes will take care of the rest to block or distribute users based on the defined country.
2. PHP GeoIP2 library API
To use PHP GeoIP2 library API, first, you need to have Composer installed. Composer is a must have tool for PHP dependency management tasks.
We have both tutorials on how to install Composer on Linux or Windows.
Then, you can download the library to your PHP project using Composer:
composer require geoip2/geoip2:~2.0
We can then include the API and start using GeoIP2 easily:
<?php
// Require Composer vendor packages
require_once 'vendor/autoload.php';
// Declare GeoIP2 library
use GeoIp2\Database\Reader;
// Load GeoIP2 / GeoLite2 database file
$reader = new Reader('/usr/local/share/GeoLite2-Country.mmdb');
// Get user country from IP
$record = $reader->country( $_SERVER['REMOTE_ADDR'] );
// Get user 2-letter country code
$countryCode = $record->country->isoCode;
Again, if you’re using proxy DNS or WAF, check the Section 1. regarding User IP to get the correct user IP.
Similar to Section 1 – example usage, after getting user’s country code, we can use PHP conditional codes to determine the preferred actions for the users matching the defined country.
Final thoughts
It is useful to know how to utilize country detection using PHP & GeoIP to enhance your website functionalities, prevent fraud and many country-based usages in practice.
Thank you for reading this article, as we will constantly update our content to provide more free and helpful PHP tutorials and guides for PHP developers.
You might also like
- How to install Composer on Windows
- How to install Composer on Linux
- Tutorial – How to install PHP GeoIP & GeoIP2