0

以下表单始终返回 true,即使域可用。谁能让我知道我做错了什么?谢谢。

<?php
function domainAvailable($domain){
$results = @dns_get_record($domain, DNS_ANY);
return empty($results);
}
if (isset($_REQUEST['domain']))  // check to see if form has been submitted
{
$domain = htmlspecialchars($_REQUEST['domain'], ENT_QUOTES, 'UTF-8');

$httpdomain = 'http://' . $domain;

    if (domainAvailable($httpdomain) == true){
        echo "$domain is not available.";
        if (domainAvailable($httpdomain) == false){
            echo "$domain is available!";   
        }
    }
}
?>
<form method="post" action="">
<div>
<label for="domain name">Suggest a Domain:</label> <input type="text" name="domain" /><br />
<input type="submit" />
</div>
</form>
4

1 回答 1

1

首先理清你的ifs:

if (domainAvailable($httpdomain)){
   echo "$domain is not available.";
} else {
   echo "$domain is available!"; 
}

此外,你可能有你的字符串倒退

if (domainAvailable($httpdomain)){
   echo "$domain is available.";
} else {
   echo "$domain is not available!"; 
}

然后,摆脱这一行:

$httpdomain = 'http://' . $domain;

您的 domainAvailable 函数可能会始终返回 true,因为http://它不是有效域名的一部分

对 htmlspecialchars 的调用也可能会导致误报。

把它们放在一起:

<?php
#CLI debugging:
if(empty($_REQUEST['domain']) && !empty($argv[1]))
    $_REQUEST['domain'] = $argv[1];

function domainAvailable($domain) {
    $results = @dns_get_record($domain, DNS_ANY);
    return empty($results);
}

if (isset($_REQUEST['domain']))  // check to see if form has been submitted
{
    $domain = htmlspecialchars($_REQUEST['domain'], ENT_QUOTES, 'UTF-8');

    if (domainAvailable($_REQUEST['domain'])) {
        echo "$domain is available.";
    } else {
        echo "$domain is not available!";
    }
}

样本输出:

$ php scratch.php google.com | more
google.com is not available!
$ php scratch.php googleasdfasdf.com. | more
googleasdfasdf.com. is available.
于 2012-05-31T01:55:06.867 回答