dns的a记录和mx记录
Validating an email address is one of the hardest feats on the web. A valid email can be marketing gold, but an invalid email address is dead weight. Not only does it require a CPU-taxing PHP regular expression ("/^[A-z0-9\._-]+"."@" . "[A-z0-9][A-z0-9-]*". "(\.[A-z0-9_-]+)*"."\.([A-z]{2,6})$/"), the regular expression can be useless even after it's validated for format if the domain doesn't exist. A regular expression simply wont do -- we need to think more low-level. What does email at any domain require? A DNS MX record. Well, PHP provides a great solution to validate that there's a MX record for the provided email address' domain.
验证电子邮件地址是网络上最艰巨的任务之一。 有效的电子邮件可以是行销黄金,但是无效的电子邮件地址是自重。 它不仅需要占用CPU资源PHP正则表达式( “ /^[A-z0-9\._-]+"."@”。“ [A-z0-9] [A-z0-9-] *“。”(\。[A-z0-9 _-] +)*“。” \。([Az] {2,6})$ /“ ),即使经过格式验证,正则表达式也可能无用如果该域不存在。 正则表达式根本行不通-我们需要考虑更底层的内容。 任何域的电子邮件都需要什么? DNS MX记录。 嗯,PHP提供了一个很好的解决方案,用于验证所提供的电子邮件地址的域是否存在MX记录。
代码 (The Code)
function domain_exists($email, $record = 'MX'){ list($user, $domain) = split('@', $email); return checkdnsrr($domain, $record);}
用法 (The Usage)
if(domain_exists('user@davidwalsh.name')) { echo('This MX records exists; I will accept this email as valid.');}else { echo('No MX record exists; Invalid email.');}
You'll see that MX is the default record check, though you could change that to A if you want. This method is not bulletproof but the function does provide a compelling enough argument for an email existing or being a fraud. If a valid email address is important for your purposes, use this function. Try it for yourself!
您会看到MX是默认的记录检查,尽管您可以根据需要将其更改为A。 此方法不是防弹的,但是该功能确实为存在的电子邮件或欺诈邮件提供了足够有说服力的论据。 如果有效的电子邮件地址对您的目的很重要,请使用此功能。 自己尝试一下!
翻译自:
dns的a记录和mx记录