以下表单始终返回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>
答案 0 :(得分: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.