2012-05-17 50 views
3

我嘗試在PHP checkdnsrr()函數來測試一個指定的主機名是否有效或無效。PHP checkdnsrr()函數的結果

下面是PHP代碼:

$url = "this_is_a_wrong_url.com"; 
if (checkdnsrr($url, 'A')) 
{ 
    echo "Domain exists."; 
} 
else 
{ 
    echo "Domain doesnot exists."; 
} 

但它返回甚至無效的網址是真實的。

我在做什麼錯?我使用PHP 5.3.5

編輯:此代碼工作在Linux機器上使用相同的PHP版本的罰款。它只在Windows機器上給出無效結果。

+0

我測試了你的代碼,它似乎很適合我。 (運行PHP 5.3.3)。也許它是PHP以外的東西造成這種情況? – vimist

+0

@ Chief17是的,你是對的。我在另一個系統(運行5.3.6)上測試過它,它按預期工作。有關如何調試導致這不能在當前系統上正常工作的任何提示? – AnonGeek

+0

在命令行中,嘗試'ping'ing /'nslookup'查看域名,看看你得到了什麼? – vimist

回答

6

$url有些ISP會無效域重定向到自己的搜索服務器。 TimeWarner/RoadRunner就是這樣一個ISP。您的代碼工作正常,但您可能需要檢查以確保它們不會解析到您的ISP的搜索服務器。首先使用gethostbyname來檢查無效的域,然後使用它來檢查。

if (checkdnsrr($url, 'A') && gethostbyname($url) != '204.232.137.207' && gethostbyname($url) !='66.152.109.110') 

或者更好的是

if(checkdnsrr($url,'A') && !in_array(gethostbyname($url),gethostbynamel('this_is_a_wrong_url.com'))) 
0

試試這個

$url = "this_is_a_wrong_url.com"; 
    if(checkdnsrr(($url),"A")) 
{ 
    echo "Domain exists."; 
} 
else 
{ 
    echo "Domain doesnot exists."; 
} 

所以brakets

+0

@MrSlayer感謝,編輯 – aurel

+0

它給語法錯誤 – AnonGeek

+0

@aurel咦?那是什麼? http://php.net/manual/en/function.checkdnsrr.php – PeeHaa

-2

我發現這裏:

http://php.net/manual/en/function.checkdnsrr.php

更新日誌

版本說明

5.3.0此功能現在可在Windows平臺上使用。加入

5.2.4 TXT類型。加入

5.0.0 AAAA類型。

這意味着你不能在Windows調用這個函數在PHP 5.3 <

+0

OP表示使用的版本是5.3.5 –

1

感謝@aynber的答案。它幫助我很多。但我認爲這對我來說並不完整。我已經修改了一些你的答案來檢查電子郵件域是否有效。實際上,它不適用於.co,.co.in,.in域名。所以,我擴大你的答案一點:)

$email = trim($_GET['email']); 

// take a given email address and split it into the username and domain. 
list($userName, $mailDomain) = split("@", $email); 
$tld = explode('.', $mailDomain,2)[1];//for php>=5.4.0 
if(checkdnsrr($mailDomain,'A') && !in_array(gethostbyname($mailDomain),gethostbynamel('this_is_a_wrong_url_xx_xx_xx.' . $tld))) { 
echo "Domain exists."; 
} else { 
echo "Domain not exists."; 
} 

@aynber,+1的答案:)。