Only execute script if entered email is from a specific domain
I am trying to create a script that will only execute its actions if the email address the user enters is from a specific domain. I created a regex that seems to work when testing it via regex utility, but when its used in my PHP script, it tells me that valid emails are invalid. In this c开发者_如何学Case, I want any email that is from @secondgearsoftware.com, @secondgearllc.com or asia.secondgearsoftware.com to echo success and all others to be rejected.
$pattern = '/\b[A-Z0-9\._%+-]+@((secondgearsoftware|secondgearllc|euro\.secondgearsoftware|asia\.secondgearsoftware)+\.)+com/';
$email = urldecode($_POST['email']);
if (preg_match($pattern, $email))
{
echo 'success';
}
else
{
echo 'opposite success';
}
I am not really sure what's futzed with the pattern. Any help would be appreciated.
Your regular expression is a bit off (it will allow foo@secondgearsoftwaresecondgearsoftware.com) and can be simplified:
$pattern = '/@((euro\.|asia\.)?secondgearsoftware|secondgearllc)\.com$/i';
I've made it case-insensitive and anchored it to the end of the string.
There doesn't seem to be a need to check what's before the "@" - you should have a proper validation routine for that if necessary, but it seems you just want to check if the email address belongs to one of these domains.
You probably need to use /\b[A-Z0-9\._%+-]+@((euro\.|asia\.)secondgearsoftware|secondgearllc)\.com/i
(note the i
at the end) in order to make the regex case-insensitive. I also dropped the +
s as they allow for infinite repetition which doesn't make sense in this case.
Here's an easy to maintain solution using regular expressions
$domains = array(
'secondgearsoftware',
'secondgearllc',
'euro\.secondgearsoftware',
'asia\.secondgearsoftware'
);
preg_match("`@(" .implode("|", $domains). ")\.com$`i", $userProvidedEmail);
Here's a couple of tests:
$tests = array(
'bob@secondgearsoftware.com',
'bob@secondgearllc.com',
'bob@Xsecondgearllc.com',
'bob@secondgearllc.net',
'bob@euro.secondgearsoftware.org',
'bob@euro.secondgearsoftware.com',
'bob@euroxsecondgearsoftware.com',
'bob@asia.secondgearsoftware.com'
);
foreach ( $tests as $test ) {
echo preg_match("`@(" .implode("|", $domains). ")\.com$`i", $test),
" <- $test\n";
}
Result (1 is passing of course)
1 <- bob@secondgearsoftware.com
1 <- bob@secondgearllc.com
0 <- bob@Xsecondgearllc.com
0 <- bob@secondgearllc.net
0 <- bob@euro.secondgearsoftware.org
1 <- bob@euro.secondgearsoftware.com
0 <- bob@euroxsecondgearsoftware.com
1 <- bob@asia.secondgearsoftware.com
I suggest you drop the regex and simply use stristr to check if it matches. Something like this should work:
<?php
// Fill out as needed
$domains = array('secondgearsoftware.com', 'secondgearllc.com');
$email = urldecode($_POST['email']);
$found = false;
for(i=0;i<count($domains);i++)
{
if ($domains[i] == stristr($email, $domains[i]))
$found = true;
}
if ($found) ...
?>
The function stristr returns the e-mail address from the part where it found a match to the end, which should be the same as the match in this case. Technically there could be something prior to the domains (fkdskjfsdksfks.secondgeartsoftware.com), but you can just insert "@domainneeded.com" to prevent this. This code is also slightly longer, but easily extended with new domains without worrying about regex.
精彩评论