DEV Community


Let's validate some DNS TXT records. πŸ‘

thinkverse profile image Kim Hallberg Originally published at ・2 min read

Here's a scenario, let's say you have users. And those users can sell domains, or maybe add their domains to their profile.

Now you can validate that they own that domain multiple ways. You could have them send a picture of the dashboard showing the domain. Or you could have them create a page just for you.

While both of these scenarios have their ups and downs. For instance the picture, the user could easily use the developer tools in the browser to add the domain. As for the page, that's just cumbersome.

Why don't we instead ask them to add a TXT record to their DNS that we can then validate? Here's a quick and easy way to validate that in PHP.

The DNS get record function.

You might think getting DNS records would be hard. But fear not, PHP has a built-in function for that. Introducing dns_get_record. πŸŽ‰

This function returns the DNS records for a given hostname in an array of associative arrays. Now we can get all records in which this function will return by default. But we want the hostname's TXT records, so let's grab only those.

$records = dns_get_record('', DNS_TXT);
Enter fullscreen mode Exit fullscreen mode

If there aren't any TXT records you will get an empty array in return.

Validating against a key.

So now that we have our $records. Let's validate that against a key. And since it returns an array we can do that pretty easily using array_filter and in_array.

So let's create an array containing our key.

$key = array('devdojo-verification=V3m0R0G00yHxVO_wWFMK1tF3nQRJ9');
Enter fullscreen mode Exit fullscreen mode

Now we can filter down our $records to only return the record that we want. The one matching our key.

$filter = array_filter($records, function ($arr) use ($key) {
    return in_array($arr['txt'], $key);
Enter fullscreen mode Exit fullscreen mode

And if we have found a match we will have a new value in our $filter containing our record. If we don't have a match our $filter will be an empty array.

Is it valid or not?

Now we can check our $filter for a record using sizeof. If we found a match then the size of our filter will be greater than zero.

print (sizeof($filter) > 0) ? 'Valid' : 'Invalid';
Enter fullscreen mode Exit fullscreen mode

Things to keep in mind.

If the hostname you're checking against adds its TXT record under and you're checking for You won't find a match. Since and will return two different records.

Keep that in mind, you need to check for the correct hostname. πŸ™‚

In conclusion.

Now you know how to get a TXT record and check for a match. As with every program though, this can be extended. You could make the check a lot more dynamic. E.g. the key could exist in your database, or in a file on your system.

You could add this to a job in a queue, and have it check every other day if the domain is still valid.

The choice is up to you, as always.

Happy coding. πŸ‘

Discussion (0)

Editor guide