PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Let's validate some DNS TXT records. 👍

Let's validate some DNS TXT records. 👍

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('example.com', DNS_TXT);

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');

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);
});

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';

Things to keep in mind.

If the hostname you're checking against adds its TXT record under www.example.com and you're checking for example.com. You won't find a match. Since www.example.com and example.com 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. 👍

Comments (0)

loading comments