PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to detect visitors by country using PHP & GeoIP

A handy guide on how to detect, distribute and block visitors by their country with PHP.

March 6, 2022
Reading Time: 4 mins read
PHP101.net - How to detect visitors by country using PHP and GeoIP

PHP101.net - How to detect visitors by country using PHP and GeoIP

Share on FacebookShare on Twitter

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

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

References

  • IBAN Official list of country codes
  • PHP.net official GeoIP documentation
  • PHP.net official documentation on geoip_country_code_by_name function
Tags: block visitors by countrydetect visitors by countryphp geoipphp geoip2redirect visitors by country
Previous Post

How to change XAMPP PHP version on Windows

Next Post

How to install PHP GeoIP extension & GeoLite2 library (databases included)

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Prerequisites
    • Detect visitors by country using PHP and GeoIP
    • 1. PHP GeoIP PECL extension
    • 2. PHP GeoIP2 library API
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.net - Tutorial - Install PHP GeoIP extension

How to install PHP GeoIP extension & GeoLite2 library (databases included)

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

Deploy a webserver with XAMPP on Windows for PHP development

How to redirect to another page using PHP

How to set MySQL charset (or MariaDB) by PHP

How to get user IP address using PHP

How to copy files using PHP

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.