使用 PHP filter_var() 或正则表达式进行邮件验证

发布于 2024-09-30 06:30:24 字数 165 浏览 8 评论 0原文

我今天了解到,可以通过两种方式验证电子邮件。一种方法是通过正则表达式,另一种方法是通过 filter_var() 函数。

如果有人能告诉我 filter_var 的验证有多强,以及关于从正则表达式切换到正则表达式的想法有任何建议或建议,那就太好了。

I have learned today, that it is possible to validate e-mail two ways. One way is by regular expression and the other is by filter_var() function.

If anyone could tell me how strong the validation with filter_var is and if there are any recommendations or suggestions on thoughts about switching from regular expression to it, then it would be just great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-10-07 06:30:24

如果您使用 filter_var ,它将验证诸如 [email  ;protected] 严格来说是有效的,但您可能不希望在您的应用程序中允许这样的地址。

如果您使用正则表达式,您可以更好地控制允许和禁止的内容。

验证电子邮件地址的最可靠方法是尝试向该地址发送电子邮件并请求收件人单击链接进行确认。这还有另一个好处 - 有人可能在输入电子邮件地址时输入错误,结果仍然是有效电子邮件地址,但只是不正确 em> 地址。

If you use filter_var it will validate things like [email protected] which is strictly speaking valid but you may not want to allow such an address in your application.

If you use a regular expression you have more control over what to allow and disallow.

The most reliable way to validate an email address though is to try to send an e-mail to it and request the recipient to click a link to confirm. This will also have another benefit - it is possible someone could make a typo while entering their e-mail address such that the result is still a valid email address, but just not the correct address.

似最初 2024-10-07 06:30:24

正如 Mark Byers 所说,filter_var 确实验证了任何严格意义上有效的内容,我认为这是一件好事。例如,使用 Gmail:
您的电子邮件地址是[电子邮件受保护]。您想在网站 x.com 上注册。您并不完全信任该网站,而是使用电子邮件地址 [email] 进行注册。受保护]。 Gmail 会自动将其重定向到您自己的收件箱。因此,如果此不受信任的网站将您的电子邮件地址出售给某人,并且您收到一封发往 [电子邮件受保护],您知道谁泄露了您的电子邮件地址!

当您编写自己的正则表达式时,您可能不会考虑这样的事情,对于使用这种语法的人(像我一样!)来说,它可能会变得非常烦人。我总是使用filter_validate()。这样,我知道有效的电子邮件地址将始终得到验证。

不管怎样,说实话:无论您使用自己的正则表达式还是filter_validate,我仍然可以编写一个将被验证的假电子邮件地址。您也可以接受任何内容并发送“激活您的帐户”电子邮件,以确保用户可以从您的网站接收和阅读电子邮件。

As Mark Byers said, filter_var does validate anything that is stricly speaing valid, and I consider this a good thing. For example, with Gmail :
Your e-mail adresse is [email protected]. You want to register on website x.com. You don't entirely trust the website, and you register with the e-mail adresse [email protected]. Gmail automaticly redirects it to your own inbox. Therefore, if this untrusted website sells your e-mail adress to someone and you receive an e-mail adressed to [email protected], you know who gave your e-mail adress away!

You may not think about things like this when you write your own regexp, and it can become really annoying for people (like me!) who use this syntax. I always use filter_validate(). This way, I know a valid e-mail adress will always be validated.

Anyway, let's be honest : whether you use your own regexp or filter_validate, I can still write a fake e-mail adress that will be validated. You may as well accept anything and send an "Activate your account" e-mail to make sure the user can receive and read e-mails from your website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文