What code to search for available domains?

Asked

Viewed 1,141 times

1

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

Look 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>
  • Take the guy’s phone out on the credits.

  • What’s the problem? If apparently you got where the problem is?

  • 2

    And the areas that are not solved by these three entities, as they are?

2 answers

3

-1

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

There were problems with some entrances like: http://php.net, said it was available, but in reality it is not. So I checked if 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 wasn’t a link like: stackkkksad, he researched and sometimes even said he was eligible, I took care of it by creating a array $domains and putting in it all domains that can be searched, and if it is not in the entered domain it returns an error.

And I finally took care of url amigavél using a function already ready, which treats strings with spaces, accents and special characters.

In the end it was like this: (here it worked perfectly)

<?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 problem because of these strange characters of the function url_amigavel, download here: 4shared

See online on:Phpfiddle Lite

  • 1

    What are these lozenges there? kkk btw did not give the -1

  • 1

    These diamonds are vector-based characters, the browser looks like this, but it works, I put in the answer a test on PHPFiddle Lite.. It’s just a ready-to-use function for handling not allowed characters in Urls

  • 1

    It would be cool if whoever gave the +1 justified the vote, for us to improve the site.

  • The function managed to handle the domains too, the question would be why the user was unable to use such code, there were small errors generating Warning.. In addition, it was only correct errow, the code is not yet good but this functional.

  • 3

    @lvcs not, nor in dream that code is functional. It has much more TLD than those of question code. Note that I have nothing against you answering, after all you are trying to help. But since the information of the question is wrong, I think it’s nice that we make it clear that it’s for visitors to be aware of the problem. If you can complement the code to work with the current domain system, it certainly takes my +1.

  • Research tip if you want to tidy up the answer (but be careful, it also has a lot of problems with the results) http://stackoverflow.com/search?q=domain+is+isregistered

  • 3

    The function url_amigável() to filter characters makes no sense. Multibyte characters for domain names are allowed. Example: http://お名前.com. Domain names can also have things like nome--qualquer.com. Notice that there are two hyphens. In practice, you do not need to filter anything. Let the user enter what they "want" because the registration company is the one who will take care of the validation. Only Sanitize entries containing protocol and URI parameters. In fact, you have already tested the operation of such vetor table? rsrs

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.