PHP - Detect visitors from EU countries

In this tutorial we demonstrate how to detect whether a website visitor comes from a European Union member country based on their IP address with PHP using an API call or a local database query.


API call

The easiest way to find if a website visit originates from a EU country is through an API call, the only prerequisite is the API client class, see the PHP code example below :

<?php
// The free open source DB-IP API client is available on GitHub : https://github.com/dbip/api-client
require "dbip-client.class.php";

// Replace with your API key
DBIP\APIKey::set("YOUR_API_KEY_GOES_HERE");

// Get IP address from the $_SERVER global array, catch forwarded address from proxies
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && $_SERVER["HTTP_X_FORWARDED_FOR"]) {
	$ipAddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
	$ipAddress = $_SERVER["REMOTE_ADDR"];
}

// Lookup IP address information
$addrInfo = DBIP\Address::lookup($ipAddress);

// Display legal warning if the visitor is from a EU member country
if ($addrInfo->isEuMember) {
	echo "You are visiting from a EU member country, please read the following legal terms";
}


Local database query

In the code example below, we assume you are using the latest database query library and already imported a CSV database to a local MySQL or MariaDB instance :

<?php
// The free open source DB-IP query library is available at : https://db-ip.com/db/#source_code
require "dbip.class.php";

// The list EU member countries ISO-3166-alpha2 codes
$euCountries = [
	"DE", "AT", "BE", "BG", "CY", "HR", "DK", "ES", "EE", "FI", "FR", "GR", "HU", "IE",
	"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "CZ", "RO", "GB", "SK", "SI", "SE"
];

// Connect to the database
$db = new PDO("mysql:host=localhost;dbname=dbip", "mylogin", "mypassword");

// Alternatively connect to MySQL using the old interface
// Comment the PDO statement above and uncomment the mysql_ calls
// below if your PHP installation doesn't support PDO :
// $db = mysql_connect("localhost", "mylogin", "mypassword");
// mysql_select_db("dbip", $db);

// Instanciate a new DBIP object with the database connection
$dbip = new DBIP($db);

// Alternatively instanciate a DBIP_MySQL object
// Comment the new statement above and uncomment below if your PHP
// installation doesn't support PDO :
// $dbip = new DBIP_MySQL($db);

// Get IP address from the $_SERVER global array, catch forwarded address from proxies
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && $_SERVER["HTTP_X_FORWARDED_FOR"]) {
	$ipAddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
	$ipAddress = $_SERVER["REMOTE_ADDR"];
}

// Lookup IP address information in local database
$addrInfo = $dbip->lookup($ipAddress);

// Display legal warning if the visitor is from a EU member country
if (in_array($addrInfo->country, $euCountries)) {
	echo "You are visiting from a EU member country, please read the following legal terms";
}

Compatibility

API DB
Free Basic Core Extended IP to Country IP to City IP to Location IP to ISP IP to Location+ISP

See also : Detect visitors from EU countries with Javascript

Was this article helpful ? Share it with others by clicking the social media buttons !