如何将电子邮件域和任何潜在的子域列入白名单以进行 wordpress 注册

How to whitelist an email domain and any potential sub domains for wordpress registration

我们目前正在另一个工作室开发的网站上做一些工作。它 运行 在 Wordpress 上,并使用一个名为 UserPro 的插件来管理其成员(登录、注册、个人资料等)

所有者希望网站普遍开放访问,但某些部分仅限会员访问。因此,他们希望只有具有特定电子邮件域的用户才能在该网站上注册。此功能适用于域和特定的子域,但是,他们很快了解到该域的子域实际上可以有无限的可能性。因此,他们需要允许来自该电子邮件域的任何潜在子域的任何人有资格在该网站上注册。

我附上了下面的代码,您可以看看它目前是如何工作的。为了所有者的隐私,我已经编辑了 URL,但其余代码完全相同。

case 'email_exists':
if (!is_email($input_value)) {
$output['error'] = __('Please enter a valid email.','userpro');
} else if (email_exists($input_value)) {
$output['error'] = __('Email is taken. Is that you? Try to <a href="#" data-                            template="login">login</a>','userpro');
}
else if($input_value){
$domain = strstr($input_value, '@');
$domains = array('@test.com' , '@test.net' , '@alpha.test.com' , '@beta.test.net', '@*.test.com', '@*.test.net');
if(!in_array($domain, $domains))
$output['error'] = __('Accounts with this email domain are not currently allowed automatic registration. You can request an account via the contact us page. ','userpro');
}
break;

于是就运行通过了。如果您有电子邮件“@test.com”或“@test.net”,它就可以正常工作。如果您在指定的子域之一(例如“@alpha.test.com”或“@beta.test.net”中有一封电子邮件,它也可以正常工作并允许注册。

但是,如果您想注册另一个人,例如“@oscar.test.com”或“@delta.test.net”,它不会让您注册。

我认为执行“@*.test.com”会允许任何可能的子域工作,但显然不会。至少,不是我想要的方式。

如有任何帮助,我们将不胜感激。

此致, 亚当

看起来像是正则表达式和 preg_match() 的工作。

有几种方法可以检查,但根据您当前的要求,只有以下 4 个选项是提交的电子邮件地址的有效结尾,

  • @test.com
  • @test.net
  • .test.com
  • .test.net

这可以用正则表达式写成 (@|\.)test\.((com)|(net))$

所以要更新你的代码,你可以替换,

else if($input_value){

   // your code here

}

有,

else if (!preg_match('~(@|\.)test\.((com)|(net))$~i', $input_value)) {
   $output['error'] = __('Accounts with this email domain are not currently allowed automatic registration. You can request an account via the contact us page. ','userpro');
}

在这里玩正则表达式 https://regex101.com/r/JtUxmZ/5