PHP script to fetch Domain Name Server information for .uk domain

What Is a Domain Name Server (DNS)?

DNS is a technology to map the names of sites with the server’s IP address. DNS is used to point domain name to server IP address.

WHOIS lookup is an internet utility that helps us to find information about a particular domain or IP address. Typically a record will contain the name and the contact information of the Registrant and the Registrar.

Let us discuss the method to get information about .uk domains:

Step 1. Assign SLD (Second Level Domain) & TLD (Top Level Domain) of domain to the php variables.

$tld = “nic.uk”;
$sld = “whois”;
$end = substr ($tld, 0 – 2); // to get last two characters of domain tld

      Step 2. Connect to the whois server using socket connection.

$whois_server = ‘whois.nic.uk’; // who is server
$fp = fsockopen ($whois_server, 43, &$err_no, &$err_str, 10); // open socket fputs ($fp, $sld.’.’.$tld); // put sld & tld data

        Step 3. Read socket data.

while (!feof ($fp)) // read up to last line of file { $buf = fgets ($fp, 128);// read line by line }

          Step 4. Find name server information.

if (ereg (‘Name servers’, $buf))
{
$dns = fgets ($fp, 128); // read data
$dns = str_replace (‘Name Server:’, ”,
$dns); // replace ‘Name Server:’ text with empty string $dns = str_replace (‘Server:’, ”, $dns); // replace ‘Server:’ text with empty string
}

            Step 5. The entire code is as follows:

if (ereg (‘Name servers’, $buf))
{
$dns = fgets ($fp, 128); // read data
$dns = str_replace (‘Name Server:’, ”, $dns); // replace ‘Name Server:’ text with empty string
$dns = str_replace (‘Server:’, ”, $dns); // replace ‘Server:’ text with empty string
}Step 5. The entire code is as follows:$tld = “nic.uk”;
$sld = “whois”;
$str_search = substr ($tld, 0 – 2);
if ($str_search == ‘uk’)
{
$whois_server = ‘whois.nic.uk’;
$fp = fsockopen ($whois_server, 43, &$err_no, &$err_str, 10);
fputs ($fp, $sld.’.’.$tld);
while (!feof ($fp))
{
$buf = fgets ($fp, 128);
if (ereg (‘Name servers’, $buf))
{
$dns = fgets ($fp, 128);
$dns = str_replace (‘Name Server:’, ”, $dns);
$dns = str_replace (‘Server:’, ”, $dns);
$domain_info['ns1'] = trim ($dns); // primary ns$dns = fgets ($fp, 128);
$dns = str_replace (‘Name Server:’, ”, $dns);
$dns = str_replace (‘Server:’, ”, $dns);
$domain_info['ns2'] = trim ($dns);// secondary ns

$dns = fgets ($fp, 128);
$dns = str_replace (‘Name Server:’, ”, $dns);
$dns = str_replace (‘Server:’, ”, $dns);
$domain_info['ns3'] = trim ($dns); // tertiary ns

$dns = fgets ($fp, 128);
$dns = str_replace (‘Name Server:’, ”, $dns);
$dns = str_replace (‘Server:’, ”, $dns);
$domain_info['ns4'] = trim ($dns); // quaternary ns
}
}
}
print_r($domain_info);

This entry was posted in PHP and tagged , , , , . Bookmark the permalink.

Leave a Reply