The following form is returning true all the time, even if the domain is available. Can anyone let me know what I’m doing wrong? Thanks.
<?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>
Start by untangling your ifs:
Also, you probably have your strings backwards
Then, get rid of this line:
Your domainAvailable function is probably going to always return true because
http://is not part of a valid domain nameThe call to htmlspecialchars is probably going to cause false negatives as well.
Put it all together:
Sample output: