星期五, 十一月 30, 2007

PHP 驗證 Email -檢查 DNS 的 MX 是否有通

驗證 Email 通常最簡單的就是判斷 '@', 要再準確一點, 就去判斷一下 DNS 的 MX 有沒有設囉~ :)

dig mx DOMAIN_NAME

這樣就可以列出 DOMAIN_NAME 的 MX 是設怎麼樣~

那 PHP 的程式要判斷 MX 有沒有設, 就用 checkdnsrr 的 function 來解決~

程式大概如下:

$email = '123@DOMAIN_NAME';
list(, $mailDomain) = split('@', $email); // 取出 DOMAIN_NAME
var_dump(checkdnsrr($mailDomain, 'MX')); // true => Email 是正確的, 反之則是 false.
相關標籤

this is comment icon [回覆]

請問windows下的php並無法使用 checkdnsrr函式,有其他解決的方法嗎?

Comment by hrf58 (10/26/2008 19:23)

this is comment icon 回 hrf58 [回覆]

試用這個? (取自 php.net)
if(!function_exists('checkdnsrr')){
function checkdnsrr($host, $type=''){
if(!empty($host)){
$type = (empty($type)) ? 'MX' : $type;
exec('nslookup -type='.$type.' '.escapeshellcmd($host), $result);
$it = new ArrayIterator($result);
foreach(new RegexIterator($it, '~^'.$host.'~', RegexIterator::GET_MATCH) as $result){
if($result){
return true;
}
}
}
return false;
}
}

Comment by Tsung (10/28/2008 00:32)
Add this page to del.icio.us Add this page to Yahoo Taiwan's bookmark Add this page to MyShare

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~