PHP is a very useful programming language, and you can use it, instead of the built-in commands like the Dig command, for querying DNS records. Do you want to know how to check DNS records using PHP? It is a simple process, and there are different PHP functions that you can use. Let’s see them.
PHP functions for checking DNS records
There are several functions that can be used for checking DNS records. For example, some can check just if the DNS records exist, others serve for only a specific type of DNS record, and others can show any type.
- checkdnsrr() – This query will see if DNS records exist for the host (hostname or IP address).
- dns_get_record() – Get the DNS record for the host (hostname or IP address). It could be any type of DNS record.
- getmxrr() – Get the MX DNS records only, for the host (hostname or IP address).
This will check many different types of DNS resource records. You can specify it with the following parameters:
- host – hostname or IP address.
- type – The particular type of DNS record that you are interested in, like A, AAAA, MX, SOA, NS, CNAME, SRV, TXT, ANY (show all), or another.
The result will be just an answer if there is such a record (“1”) or there is no such a record (“0”).
$all_records_check = checkdnsrr ( ‘google.com’, ‘ANY’ );
This will check the host google.com for any type of DNS records, and the result is that it does have DNS records (“1”).
$mx_records_check = checkdnsrr ( ‘google.com’, ‘MX’ );
This will check the host google.com for MX DNS records, and the result is that it does have MX records (“1”).
This PHP function will serve you if you actually need to check the content of the DNS records. It will retrieve the DNS record or records that you asked for from the DNS name server. You have a few parameters that we can use as before:
- hostname – Here, you can put the hostname like “google.com”.
- type – With this parameter, you can specify the type of DNS record that you want to get. Here are some of them that you can use: DNS_A, DNS_AAAA, DNS_CNAME, DNS_HINFO, DNS_SOA, DNS_NS, DNS_PTR, DNS_MX, DNS_TXT, DNS_SRV, DNS_NAPTR, DNS_A6, or DNS_ANY.
- authns – This will populate the authoritative name server with recource records.
- addtl – This will populate the authoritative name server with aditional records.
- raw – Query only the requested type of DNS record, instead of looping type by type before going with the additional information.
This is probably the most used PHP function for checking DNS records because it will return you the actual DNS records. Not like in the previous case, just a confirmation if they exist or not.
$get_any_records = dns_get_record(“google.com”, DNS_A+DNS_NS);
In this case, we are checking google.com for the A records and the NS records.
This works only with MX records. This function will get us the MX records for the domain name we are checking. The parameters that can be used with this are below:
- hostname – The hostname of the target
- mxhosts – Here, we will see an array of all the available MX records that the query can find.
- weight – With this one, it will show the weight information it can find about the order.
$get_mx_records = getmxrr(“google.com“, $mx_array);
With this code we will see the MX records for google.com, organized in an array.
PHP serves to get DNS records. It is very easy to use. Try the functions that we mentioned before with the hostname that you need and the particular DNS records.