InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Performing DNS Lookups in PHP
PHP

Performing DNS Lookups in PHP

InfinityCoder December 20, 2016

You want to look up a domain name or an IP address.

Use gethostbyname() and gethostbyaddr():

1
2
$ip   = gethostbyname('www.example.com'); // 93.184.216.119
$host = gethostbyaddr('93.184.216.119'); // www.example.com

You can generally trust the address returned by gethostbyname(), but you can’t trust the name returned by gethostbyaddr().

A DNS server with authority for a particular IP address can return any hostname at all. Usually, administrators set up DNS servers to reply with a correct hostname, but a malicious user may configure her DNS server to reply with incorrect hostnames.

One way to combat this trickery is to call gethost byname() on the hostname returned from gethostbyaddr() and make sure the name resolves to the original IP address.
If either function can’t successfully look up the IP address or the domain name, it doesn’t return false, but instead returns the argument passed to it. To check for failure, do this:

1
2
3
4
$host = 'this is not a good host name!';
if ($host == ($ip = gethostbyname($host))) {
    // failure
}

This assigns the return value of gethostbyname() to $ip and also checks that $ip is not equal to the original $host.
Sometimes a single hostname can map to multiple IP addresses. To find all hosts, use gethostbynamel():

1
2
$hosts = gethostbynamel('www.yahoo.com');
print_r($hosts);

This prints something like the following (the specific IP addresses may vary based on your location):

1
2
3
4
5
Array
(
   [0] => 98.139.183.24
   [1] => 98.139.180.149
)

In contrast to gethostbyname() and gethostbyaddr(), gethostbynamel() returns an array, not a string.
You can also do more complicated DNS-related tasks. For instance, you can get the MX records using getmxrr():

1
2
3
4
getmxrr('yahoo.com', $hosts, $weight);
for ($i = 0; $i < count($hosts); $i++) {
     echo "$weight[$i] $hosts[$i]\n";
}

This prints:
Whereas gethostbyname() retrieves IPv4 A records, and getmxrr() retrieves MX records, the dns_get_record() function retrieves whichever type of DNS record you specify.

This is useful, for example, to retrieve IPv6 AAAA records, as follows:

1
2
$addrs = dns_get_record('www.yahoo.com', DNS_AAAA);
print_r($addrs);

This prints something like the following (again, the specifics will vary based on your location):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Array
(
    [0] => Array
        (
             [host] => ds-any-fp3-real.wa1.b.yahoo.com
             [type] => AAAA
             [ipv6] => 2001:4998:f00b:1fe::3001
             [class] => IN
             [ttl] => 34
        )
    [1] => Array
        (
             [host] => ds-any-fp3-real.wa1.b.yahoo.com
             [type] => AAAA
             [ipv6] => 2001:4998:f00d:1fe::3001
             [class] => IN
             [ttl] => 34
        )
    [2] => Array
        (
             [host] => ds-any-fp3-real.wa1.b.yahoo.com
             [type] => AAAA
             [ipv6] => 2001:4998:f00d:1fe::3000
             [class] => IN
             [ttl] => 34
        )
    [3] => Array
        (
             [host] => ds-any-fp3-real.wa1.b.yahoo.com
             [type] => AAAA
             [ipv6] => 2001:4998:f00b:1fe::3000
             [class] => IN
             [ttl] => 34
        )
)

Each element of the returned array is a subarray containing information about the record type, hostname, IPv6 address, record class, and the TTL—how long the record is cacheable for.
Read the manual page for dns_get_record() to learn how to indicate which type of DNS record you are interested in.
To perform zone transfers, dynamic DNS updates, and more, see PEAR’s Net_DNS2 package.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Modifying a File in Place Without a Temporary File in PHP
You want to change a file without using a temporary …

Modifying a File in Place Without a Temporary File in PHP

Setting XSLT Parameters from PHP in PHP
You want to set parameters in your XSLT stylesheet from …

Setting XSLT Parameters from PHP in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap