IP-to-Country DNS Mapping Service

Want to block email from specific countries? Want to check a visitor’s country within your PHP script? It’s all quite easy using this free IP-to-country DNS mapping service!

What Is IP-to-Country DNS Mapping?

Put simply it’s a DNS service that will respond to lookups confirming whether or not the specified IP address resides in a given country. This lets us do two very useful things: we can block email based on the sender’s country; and we can get a visitor’s country within our PHP script and take action based upon it.

The particular DNS service that I am going to discuss is The DNSBL countries.nerd.dk.

Using as a DNSBL

A DNSBL is a “blacklist” of IP addresses reputed to send email spam. Mail servers can be configured to reject or flag messages that have been sent from an IP address listed on one or more such lists.

The IP-to-country DNS does not list spammers, it simply lets us lookup an IP to determine whether it resides in a given country. However, you can use this like a DNSBL if you want to block ALL email from a given country or countries. If you wanted to block all email from China then configure your mail server to use cn.countries.nerd.dk as a DNSBL. Then if the sending mail server’s IP address is listed in cn.countries.nerd.dk it will be assumed to be Chinese and the email refused.

Find the Country of a Given IP Address

Much more useful from my point of view is having the ability to perform a single lookup and find the country of a given IP address. This is made possible using the zz.countries.nerd.dk DNS zone.

Here’s an example of why I use it: I had set up a forum for a popular UK location, which would be of little interest to anyone outside the UK, and it received a large amount of spam from non-UK traffic (China, Ukraine etc…). CAPTCHAs on the registration page didn’t work. So I took the decision that I would capture any non-UK traffic and advise them to contact me to obtain access to the website. Or rather I would identify UK IP addresses and let them through, all others would be blocked and shown the message.

Other uses would be to guess what language to display your content in, or show content most relevant to the user’s location, etc…

Perform a DNS Lookup with PHP

The little snippet of PHP code below will perform a DNS lookup at zz.countries.nerd.dk and return the ISO 3166 number codes encoded in the last two octets of the reply. For example a lookup of an IP address in Denmark would give a reply of 127.0.0.208 (208=Denmark), while a US IP would give 127.0.3.72 (3*256+72=840=USA).

// Reverse the visitor's IP address
$reversed_ip = implode('.', array_reverse(explode('.', $_SERVER['REMOTE_ADDR'])));
 
// The DNS zone
$zone = 'zz.countries.nerd.dk';
 
// The full host to query
$full_host = $reversed_ip . '.' . $zone;
 
// Perform the lookup
$result = dns_get_record($full_host, DNS_A);
 
// The IP address that we want
$return_ip = $result[0]['ip'];

This is straightforward for identifying traffic from a specific country. For example UK IP addresses would have a $return_ip of 127.0.3.58. Remember that your visitor’s IP may not actually be listed, so check that $return_ip is not empty before performing any logic based upon it.

Here is a list of return IP values mapped to country codes so determining a country is a simple process.

Useful Links

Leave a Reply

Your email address will not be published. Required fields are marked *