[英]Why does filter_var($email, FILTER_VALIDATE_EMAIL) allow test@test?
I was just setting up the validation for a form in which I decided to try using the filter_var function to check the validity of my email address. 我刚刚设置了一个表单的验证,我决定尝试使用filter_var函数来检查我的电子邮件地址的有效性。 I can not find out what filter_var actually allows anywhere though (since the documentation is very simple), and I found out that it is allowing an email address like test@test. 我无法找到filter_var实际允许的任何地方(因为文档很简单),我发现它允许一个像test @ test这样的电子邮件地址。 Doesn't there have to be a .com, .net etc... in the domain? 在域中是否必须有.com,.net等...
The behavior has changed somewhere around April. 这种行为在四月左右发生了变化。 See bug #49576 and revision 297350 . 请参阅错误#49576和修订版297350 。
That e-mail is indeed invalid, or at least that's what the PHP developers understood. 那封电子邮件确实无效,或者至少是PHP开发人员所理解的。 The source carries this notice: 来源载有此通知:
/*
* The regex below is based on a regex by Michael Rushton.
* However, it is not identical. I changed it to only consider routeable
* addresses as valid. Michael's regex considers a@b a valid address
* which conflicts with section 2.3.5 of RFC 5321 which states that:
*
* Only resolvable, fully-qualified domain names (FQDNs) are permitted
* when domain names are used in SMTP. In other words, names that can
* be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
* in Section 5) are permitted, as are CNAME RRs whose targets can be
* resolved, in turn, to MX or address RRs. Local nicknames or
* unqualified names MUST NOT be used.
The changelog mentions this bug fix for PHP 5.3.3 and PHP 5.2.14. 更改日志提到了PHP 5.3.3和PHP 5.2.14的此错误修复。
It is a valid email address. 这是一个有效的电子邮件地址。 It isn't going to work on the Internet (at least, not today), but it is fine for a local address. 它不会在互联网上工作(至少,不是今天),但它适用于本地地址。
I would assume that the developers are taking the sensible approach to checking email addresses and not building themselves a system that is guaranteed to go out of date as soon as a new TLD is introduced. 我认为开发人员正在采取明智的方法来检查电子邮件地址,而不是建立自己的系统,一旦引入新的TLD,该系统就会保证过时。 We have enough email address syntax checkers that reject foo@example.museum
as it is. 我们有足够的电子邮件地址语法检查程序,拒绝foo@example.museum
。
test@test is syntactically valid. test @ test在语法上是有效的。
From RFC 5321: 来自RFC 5321:
In the case of a top-level domain used by itself in an email address, a single string is used without any dots. 如果在电子邮件地址中使用顶级域名,则使用单个字符串而不使用任何点。
Only after this does it say: 只有在这之后它才会说:
Only resolvable, fully-qualified domain names (FQDNs) are permitted when domain names are used in SMTP. 在SMTP中使用域名时,仅允许可解析的完全限定域名(FQDN)。 In other words, names that can be resolved to MX RRs or address (ie, A or AAAA) RRs (as discussed in Section 5) are permitted, as are CNAME RRs whose targets can be resolved, in turn, to MX or address RRs. 换句话说,允许解析为MX RR或地址(即A或AAAA)RR的名称(如第5节所述),CNAME RR的目标可以依次解析为MX或地址RR 。 Local nicknames or unqualified names MUST NOT be used. 不得使用本地昵称或不合格的名称。
This does not necessarily preclude TLD-only domain names. 这并不一定排除仅限TLD的域名。 In fact, run the following code: 实际上,运行以下代码:
checkdnsrr('ua', 'MX') // Returns true
getmxrr('ua', $array) // Returns true
TLD-only domain names (can) have MX records and are in use: http://www.to/ is an example. 仅限TLD的域名(可以)有MX记录并且正在使用中: http : //www.to/就是一个例子。 And here's some valid TLD-only domain name email addresses: 这里有一些有效的TLD域名电子邮件地址:
vince@ai 文斯@ AI
paul@io 保罗@ IO
root@km 根@公里
joost@tk Joost的@ TK
admin@tt 管理员@ TT
hostmaster@ua hostmaster @ UA
Source of example email addresses: Tony Finch – TLDs with MXs 示例电子邮件地址的来源: Tony Finch - 带有MX的TLD
No, test
can be a local / internal network domain, so that would work. 不, test
可以是本地/内部网络域,因此可以工作。 I like it that it correctly validates wrikken@localhost
when developing for instance. 我喜欢它在开发时正确验证了wrikken@localhost
。
A normal nonexistentdomain.foo
would have the same problem. 正常的nonexistentdomain.foo
会有同样的问题。 If you want to test whether something is delivarable to a host, use getmxrr
(and it that fails fall back to gethostbyname()
). 如果要测试某些东西是否可以被主机getmxrr
,请使用getmxrr
(并且它将失败回到gethostbyname()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.