What code to search for available domains?

1

I have searched the net but found nothing that would help have a PHP script that would check the searched domain.

See what I got:

<? 
function whois ($domain, $tipo) { 

$server[0] = "whois.registro.br"; //--> Domínios Nacionais 
$server[1] = "whois.internic.net"; //--> Domínios .com, .net, .org, .edu 
$server[2] = "whois.networksolutions.com"; //--> Domínios .aero, .arpa, .biz, .coop, .info, .int, .museum 

$domain = strtolower($domain); 
if (trim($domain) <> "") { 
$domain = trim($domain); 
$final = substr($domain, -4); 
// Verifica dominios do brasil 
if (substr("$domain", -3) == ".br") { 
$br = fsockopen($server[0], 43, $errno, $errstr, 30); 
if (!$br) { 
$dados .= "$errstr ($errno)"; 
} else { 
fputs($br, "$domain\r\n"); 
while (!feof($br)) { 
$buffer .= fread($br,128); 
} 
if ($tipo == 1) { 
if (strpos($buffer, "No match for") > 0) $dados .= false; else $dados .= true; 
} else { 
$dados .= str_replace("\n","<BR>\n",trim($buffer)); 
} 
fclose ($br); 
} 

// Verifica domínios .com, .net, .org, .edu 
} elseif (($final == '.com') OR ($final == '.net') OR ($final == '.org') OR ($final == '.edu')) { 
$internic = fsockopen($server[1], 43, $errno, $errstr, 30); 
if (!$internic) { 
$dados .= "$errstr ($errno)"; 
} else { 
fputs($internic, "$domain\r\n"); 
while (!feof($internic)) { 
$buffer .= fread($internic,128); 
} 
if ($tipo == 1) { 
if (strpos($buffer, "No match for") > 0) $dados .= false; else $dados .= true; 
} else { 
if (strpos($buffer, "No match for") > 0) { 
$dados .= "<pre>" . trim($buffer) . "</pre>"; 
} else { 
$servidor = substr($buffer, strpos($buffer, "Whois Server:")+14, strlen($buffer)); 
$servidor = substr($servidor, 0, strpos($servidor, "\n")); 
$entidade = fsockopen("$servidor", 43, $errno, $errstr, 30); 
if (!$entidade) { 
$dados .= "$errstr ($errno)"; 
} else { 
$buffer .= "-------------------------------------------------------------------------------<BR> <BR>"; 
fputs($entidade, "$domain\r\n"); 
while (!feof($entidade)) { 
$buffer .= fread($entidade,128); 
} 
$dados .= str_replace("\n","<BR>\n",trim($buffer)); 
fclose ($entidade); 
} 
} 
} 
fclose ($internic); 
} 

// Verifica os outros 
} else { 
$network = fsockopen($server[2], 43, $errno, $errstr, 30); 
if (!$network) { 
$dados .= "$errstr ($errno)"; 
} else { 
fputs($network, "$domain\r\n"); 
while (!feof($network)) { 
$buffer .= fread($network,128); 
} 
if ($tipo == 1) { 
if (strpos($buffer, "NOT FOUND") > 0) $dados .= false; else $dados .= true; 
} else { 
$dados .= str_replace("\n","<BR>\n",trim($buffer)); 
} 
fclose ($network); 
} 
} 
} 
return $dados; 
} 

?> 
<form method=get>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
<input type=text name=dominio size=30>
<input type=submit value=Consultar>
</font> 
</form>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
<? if (strlen($_GET["dominio"]) > 0) { ?>
</font> 
<HR>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Resposta simplificada:</b><BR>
<BR>
O Domínio <b> 
<?=$_GET["dominio"]?>
</b> 
<? if (whois($_GET["dominio"],1) == 1) echo "<br><b><img src=registrado.jpg width=32 height=32 align=absmiddle> <font color=#FF0000 size=1 face=Verdana, Arial, Helvetica, sans-serif>DOMINIO REGISTRADO</font></b>"; else echo "<br><b><img src=livre.jpg width=32 height=32 align=absmiddle><font color=#66CC66 size=1 face=Verdana, Arial, Helvetica, sans-serif>DOMINIO 
LIVRE </b> </font>";?>
</font> 
<HR>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Resposta completa:</b><BR>
<BR>
<?= whois($_GET["dominio"],2)?>
<? } ?>
<br>
<br>
<br>
    
asked by anonymous 28.01.2016 / 19:04

2 answers

3

Maybe checkdnsrr will help you: link

if (checkdnsrr("www.globo.com.br", "A")) {
  echo "Domínio encontrado";
} else {
  echo "Domínio não encontrado";
}
    
27.09.2017 / 14:57
-1

Well, the script was practically correct, I just removed the detailed results part and did a better check of the domain entered.

There were problems with some incomers like: http://php.net , said that it was available, but in reality it is not. So I checked whether there is http:// or https:// and www. in the searched domain, if yes I removed them and then yes I did the search.

Another problem was when I came in with something that was not a link like: stackkkksad , it searched and sometimes even said it was available, I dealt with it by creating a array $domains and placing all domains that can be searched in it, and if it does not have the typed domain it returns an error.

And finally I dealt with url amiable using a ready-made function, which treats strings with spaces, accents and special characters.

At the end it looked like this: (it worked perfectly here)

<?php

function url_amigavel($string)
{
    $table = array(
        '�'=>'S', '�'=>'s', '�'=>'Dj', '�'=>'Z',
        '�'=>'z', 'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c',
        '�'=>'A', '�'=>'A', '�'=>'A', '�'=>'A', '�'=>'A',
        '�'=>'A', '�'=>'A', '�'=>'C', '�'=>'E', '�'=>'E',
        '�'=>'E', '�'=>'E', '�'=>'I', '�'=>'I', '�'=>'I',
        '�'=>'I', '�'=>'N', '�'=>'O', '�'=>'O', '�'=>'O',
        '�'=>'O', '�'=>'O', '�'=>'O', '�'=>'U', '�'=>'U',
        '�'=>'U', '�'=>'U', '�'=>'Y', '�'=>'B', '�'=>'Ss',
        '�'=>'a', '�'=>'a', '�'=>'a', '�'=>'a', '�'=>'a',
        '�'=>'a', '�'=>'a', '�'=>'c', '�'=>'e', '�'=>'e',
        '�'=>'e', '�'=>'e', '�'=>'i', '�'=>'i', '�'=>'i',
        '�'=>'i', '�'=>'o', '�'=>'n', '�'=>'o', '�'=>'o',
        '�'=>'o', '�'=>'o', '�'=>'o', '�'=>'o', '�'=>'u',
        '�'=>'u', '�'=>'u', '�'=>'y', '�'=>'y', '�'=>'b',
        '�'=>'y', 'R'=>'R', 'r'=>'r',
    );
    // Traduz os caracteres em $string, baseado no vetor $table
    $string = strtr($string, $table);
    // converte para min�sculo
    $string = strtolower($string);
    // remove caracteres indesej�veis (que n�o est�o no padr�o)
    $string = preg_replace("/[^a-z0-9_\s-\.]/", "", $string);
    // Remove m�ltiplas ocorr�ncias de h�fens ou espa�os
    $string = preg_replace("/[\s-]+/", " ", $string);
    // Transforma espa�os e underscores em h�fens
    $string = preg_replace("/[\s_]/", "-", $string);
    // retorna a string
    return $string;
}

function whois($domain, $tipo) {
    $buffer = "";
    $dados = "";
    $server[0] = "whois.registro.br"; //--> Domínios Nacionais 
    $server[1] = "whois.internic.net"; //--> Domínios .com, .net, .org, .edu 
    $server[2] = "whois.networksolutions.com"; //--> Domínios .aero, .arpa, .biz, .coop, .info, .int, .museum 
    $domExist = false;
    $domains = array(".com", ".net", ".org", ".edu", ".aero", ".arpa",
        ".biz", ".coop", ".info", ".int", ".museum", ".br");
    $domain = strtolower($domain);
    $domain = url_amigavel($domain);
    echo "<script>alert({$domain});</script>";

    $exist = strripos($domain, "http://");
    if($exist === false) { } else {
        $domain = str_replace("http://", "", $domain);
    }
    $exist = strripos($domain, "https://");
    if($exist === false) { } else {
        $domain = str_replace("https://", "", $domain);
    }
    $exist = strripos($domain, "www.");
    if($exist === false) { } else {
        $domain = str_replace("www.", "", $domain);
    }

    for($i = 0; $i < count($domains); $i++) {
        $exist = strripos($domain, $domains[$i]);
        if($exist === false) {
            $domExist = false;
        } else {
            $domExist = true;
            break;
        }
    }
    if($domExist) {
        if (trim($domain) <> "") {
            $domain = trim($domain);
            $final = substr($domain, -4);
    // Verifica dominios do brasil 
            if (substr("$domain", -3) == ".br") {
                $br = fsockopen($server[0], 43, $errno, $errstr, 30);
                if (!$br) {
                    $dados .= "$errstr ($errno)";
                } else {
                    fputs($br, "$domain\r\n");
                    while (!feof($br)) {
                        $buffer .= fread($br, 128);
                    }
                    if ($tipo == 1) {
                        if (strpos($buffer, "No match for") > 0)
                            $dados .= false;
                        else
                            $dados .= true;
                    } else {
                        $dados .= str_replace("\n", "<BR>\n", trim($buffer));
                    }
                    fclose($br);
                }

    // Verifica domínios .com, .net, .org, .edu 
            } elseif (($final == '.com') OR ( $final == '.net') OR ( $final == '.org') OR ( $final == '.edu')) {
                $internic = fsockopen($server[1], 43, $errno, $errstr, 30);
                if (!$internic) {
                    $dados .= "$errstr ($errno)";
                } else {
                    fputs($internic, "$domain\r\n");
                    while (!feof($internic)) {
                        $buffer .= fread($internic, 128);
                    }
                    if ($tipo == 1) {
                        if (strpos($buffer, "No match for") > 0)
                            $dados .= false;
                        else
                            $dados .= true;
                    } else {
                        if (strpos($buffer, "No match for") > 0) {
                            $dados .= "<pre>" . trim($buffer) . "</pre>";
                        } else {
                            $servidor = substr($buffer, strpos($buffer, "Whois Server:") + 14, strlen($buffer));
                            $servidor = substr($servidor, 0, strpos($servidor, "\n"));
                            $entidade = fsockopen("$servidor", 43, $errno, $errstr, 30);
                            if (!$entidade) {
                                $dados .= "$errstr ($errno)";
                            } else {
                                $buffer .= "-------------------------------------------------------------------------------<BR> <BR>";
                                fputs($entidade, "$domain\r\n");
                                while (!feof($entidade)) {
                                    $buffer .= fread($entidade, 128);
                                }
                                $dados .= str_replace("\n", "<BR>\n", trim($buffer));
                                fclose($entidade);
                            }
                        }
                    }
                    fclose($internic);
                }

    // Verifica os outros 
            } else {
                $network = fsockopen($server[2], 43, $errno, $errstr, 30);
                if (!$network) {
                    $dados .= "$errstr ($errno)";
                } else {
                    fputs($network, "$domain\r\n");
                    while (!feof($network)) {
                        $buffer .= fread($network, 128);
                    }
                    if ($tipo == 1) {
                        if (strpos($buffer, "NOT FOUND") > 0)
                            $dados .= false;
                        else
                            $dados .= true;
                    } else {
                        $dados .= str_replace("\n", "<BR>\n", trim($buffer));
                    }
                    fclose($network);
                }
            }
        }
        return $dados;
    } else {
        return 1;
    }

}
?> 
<form method=get>
    <font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type=text name=dominio size=30>
    <input type=submit value=Consultar>
    </font> 
</form>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
<?php if (isset($_GET["dominio"]) and strlen($_GET["dominio"]) > 0) { ?>
</font> 
<HR>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Resposta simplificada:</b><BR>
<BR>
O Domínio <b> 
<?php if(isset($_GET["dominio"])) echo url_amigavel($_GET["dominio"]); ?>
</b> 
<?php if (whois($_GET["dominio"],1) == 1) echo "<br><b><img src=registrado.jpg width=32 height=32 align=absmiddle> <font color=#FF0000 size=1 face=Verdana, Arial, Helvetica, sans-serif>DOMINIO REGISTRADO OU INVÀLIDO</font></b>"; else echo "<br><b><img src=livre.jpg width=32 height=32 align=absmiddle><font color=#66CC66 size=1 face=Verdana, Arial, Helvetica, sans-serif>DOMINIO 
LIVRE </b> </font>";?>
</font> 
<HR>
<?php } ?>

If I have a problem with these strange characters in the url_amigavel function, download here: 4Shared

I look online at PHPFiddle Lite

    
28.01.2016 / 21:17