Extract Domain Name From Hostname in PHP

When fighting spammers or other unwanted traffic it’s useful to be able to block a top level domain, such as ukhost4u.com or uk2.net. This needs to be extracted from the full hostname, which might be something like 123-456-789-10.ukhost4u.com. To do that you’ll need a combination of a PHP built-in function and a regular expression.

Why Block a Top-Level Domain?

Blocking a top-level domain sounds drastic, and in a way it it. But in certain situations it makes perfect sense. For example, I was running a forum and found that the majority of spam was posted by bots, which would be hosted by well-known web hosts. The culprits would have hostnames like 123-456-789-10.ukhost4u.com, which made them fairly easy to spot. You can perform a lookup on an offending IP address and get its host.

Believing that none of these web hosts operated as ISPs, it was a fairly safe assumption that any traffic originating from them would be malicious at worst, or unwanted at best. So that’s why I found it necessary to block whole top-level domains.

Isolate the Domain from the Hostname

I quickly found that there was no pre-existing PHP method that would simply return the domain from the hostname or the IP address. It is therefore necessary to first get the hostname from the IP address, then parse it using a regular expression to extract the domain.

// Get the hostname from the IP address (e.g. 123-456-789-10.ukhost4u.com)
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
// Isolate the domain from the hostname (e.g. ukhost4u.com)
$domain = preg_replace('#^(?:.+?\.)+(.+?\.(?:co\.uk|com|net|org))#', '$1', $host);

The regular expression isn’t perfect, and it will need tweaking to add extra TLDs as you see fit, but the majority of web hosts stick with the common ones.

So now you’ve got the visitor’s domain you can compare it with a list of “known offenders” (i.e. web hosts!) and do with it as you wish. Personally I blocked the following domains:

$web_hosts = array(

Usefuly Links

Leave a Reply

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