Reputation: 77
We're currently doing some work on a website that has been developed by another studio. It runs on Wordpress and uses a plugin called UserPro for managing it's members (login, registration, profile etc.)
The owners want the website to have open access generally but some parts are restricted to members only. They therefore wanted only users with specific email domains to be able to register on the website. This feature works for domains and specific sub domains, however, they soon learned that there can actually be an endless amount of possibilities for the subdomains of this domain. So they need to allow anyone from any potential subdomain of this email domain to be eligible to register on the website.
I've attached the code below for you to see how it currently works. I've edited the URLs for the privacy of the owners but the rest of the code is exactly the same.
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;
So to run through it. If you have an email '@test.com' or '@test.net' it works fine. If you have an email at one of the specified sub domains such as '@alpha.test.com' or '@beta.test.net' it also works fine and allows registration.
However, if you wanted to sign up with another one such as '@oscar.test.com' or '@delta.test.net' it wont let you register.
I thought doing '@*.test.com' would allow for any possible subdomain to work but it clearly doesn't. At least, not in the way I'm going about it.
Any assistance regarding this matter would be greatly appreciated.
Regards, Adam
Upvotes: 0
Views: 729
Reputation: 1963
Looks like a job for regex and preg_match()
.
There's several ways to check, but based on your current requirements only the following 4 options are valid endings to the submitted email address,
This can be written in regex as (@|\.)test\.((com)|(net))$
So to update your code, you can replace,
else if($input_value){
// your code here
}
with,
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');
}
Play with the regex here https://regex101.com/r/JtUxmZ/5
Upvotes: 2