如何创建限制注册以“@domain.com”结尾的电子邮件地址

问题描述 投票:0回答:2

除了以“@domain.com”结尾的电子邮件之外,其他电子邮件地址不应在我的网站注册页面上注册

php wordpress .htaccess
2个回答
1
投票

您可以在主题的functions.php 文件中使用此代码。只需将domain.com更改为其他名称,例如gmail.com,outlook.com等

// Custom domain registration only

function custom_domain_email($login, $email, $errors ){
 $accepted_domains_emails = array("domain.com");// allowed domains
 $valid = false; // sets default validation to false
 foreach( $accepted_domains_emails as $d ){
  $d_length = strlen( $d );
  $accepted_email_domain = strtolower( substr( $email, -($d_length), $d_length));
 if( $accepted_email_domain == strtolower($d) ){
  $valid = true;
  break;
 }
 }
 // Show error message
 if( $valid === false ){

$errors->add('domain_whitelist_error',__( 'Registration is only allowed from domains.com domain only.' ));
 }
}
add_action('register_post', 'custom_domain_email',10,3 );

0
投票

最简单的方法是使用正则表达式并将给定的电子邮件地址与其进行匹配。这是一个简单的演示,使用四个地址并为每个地址决定是否接受或拒绝:

<?php
$emailAddresses = [
  "[email protected] ",
  "   [email protected]",
  "[email protected]", 
  "personD@ivalid",
  "personE"
];

array_walk($emailAddresses, function($emailAddress) {
  var_dump(preg_match('/@good\.com$/', trim($emailAddress)) ? "accepted" : "rejected");
});

输出显然是:

string(8) "accepted"
string(8) "accepted"
string(8) "rejected"
string(8) "rejected"
string(8) "rejected"

所以你真正需要的就是这个命令:

preg_match('/@domain.com$/',trim($emailAddress))

© www.soinside.com 2019 - 2024. All rights reserved.