bookmark_borderBest way to get hostname with PHP

Sometimes you have a PHP application and multiple devices, and you need a fast and reliable way to get the hostname of a server. If that is your case, here you will learn the best way to get a hostname with PHP. You can find it if you know the IP address or the local machine is the host.

Gethostbyaddr() PHP function

You can get the hostname if you already have the IP address of it with the gethostbyaddr() PHP function. PHP will perform a reverse DNS lookup for that IP address, and it will show the hostname related to it. This is its syntax:

gethostbyaddr(string $ip): string|false

The result will be either the string of hostname or a false value.

Example code:


$hostname = gethostbyaddr(“”);

echo “Hostname is: “. $hostname;


In this case, we are checking the IP address, which you can see in the result, belong to the host with hostname –

There is another very similar function that is the mirror of this one, and it is called the gethostbyname() function. As you could have guessed, you can use it if you have the hostname already, and you need to find the IPv4 address of it.

gethostname() PHP function

The gethostname() PHP function will give you the hostname of the localhost. Its syntax is the following:

gethostname(): string|false

It is an inbuilt function in PHP. There are no additional parameters, and the result can be either string with the hostname if successful or false if not.

It works on PHP 5.3 and newer versions.

Example code:


echo gethostname();


Get IPv4 address with PHP explained


After this, now you know the best way to get the name of the host using PHP. It is not hard to use it. So go on and try it right away!

bookmark_border​How to check DNS records using PHP

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).

​checkdnsrr() function 

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”). 

Code example:


$all_records_check = checkdnsrr ( ‘’, ‘ANY’ );

echo $all_records_check;


This will check the host for any type of DNS records, and the result is that it does have DNS records (“1”). 


$mx_records_check = checkdnsrr ( ‘’, ‘MX’ );

echo $mx_records_check;


This will check the host for MX DNS records, and the result is that it does have MX records (“1”).

​dns_get_record() function

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 “”. 
  • 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. 

Code example:


$get_any_records = dns_get_record(“”, DNS_A+DNS_NS);



In this case, we are checking for the A records and the NS records.  

​getmxrr() functions

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. 

Code example:


$get_mx_records = getmxrr(““, $mx_array);



With this code we will see the MX records for, organized in an array.

Get IPv4 address with PHP explained


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.