【问题标题】:PHP filter_var and RFC 5322PHP filter_var 和 RFC 5322
【发布时间】:2015-04-01 13:01:45
【问题描述】:

PHP 函数 filter_var('bob@example.com', FILTER_VALIDATE_EMAIL) 是否使用标准 RFC 5322 验证电子邮件?

【问题讨论】:

标签: php filter-var rfc5322


【解决方案1】:

作为filter_var 使用的正则表达式的作者,我可以确认它没有全面使用 RFC 5322(具体来说,它不允许 cmets 和折叠空格) .

VolkerK 链接到的文章包含更新的验证——包括更准确的 RFC 5322 实现以及 RFC 5321 的实现(我认为更合适的标准)——但 filter_var 尚未更新为合并改进。

【讨论】:

    【解决方案2】:

    implementation code of that filter有评论:

    void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
    {
        /*
         * 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.
         *
         * This regex does not handle comments and folding whitespace.  While
         * this is technically valid in an email address, these parts aren't
         * actually part of the address itself.
         *
         * Michael's regex carries this copyright:
         *
         * Copyright © Michael Rushton 2009-10
         * http://squiloople.com/
         * Feel free to use and redistribute this code. But please keep this copyright notice.
         *
         */
    

    “原始”来源很可能是:http://squiloople.com/2009/12/20/email-address-validation/

    /**
       * Validate an email address using RFC 5322
       *
    ...
    

    因此,您提出了索赔,并且有人修复了所谓的错误,并且...
    ...除此之外我不知道 ;-)

    【讨论】:

    • “观察到的错误”实际上并不是错误,只是对如何解释标准的分歧。根据上面引用的部分,我继续声称 a@b 在语法上 是有效的:“如果在电子邮件地址中自己使用顶级域,则使用单个字符串而不使用任何点。”
    • 对不起,我的意思是涉嫌;-)
    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多