I have a registration form that uses any kind of emails for registration. I want to restrict it to company mail id's only. In other words, no free email service provider's mail id would work for registration.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Since you have not provided any additional information as to how E-mail addresses are being defined and/or entered into a form or not, am submitting the following using PHP's preg_match()
function, along with b
and i
pattern delimiters and an array.
b
- word boundary
i
- case insensitive
- http://php.net/manual/en/function.preg-match.php
The following will match against "gmail" or "Gmail" etc. should someone want to trick the system.
Including Hotmail, Yahoo. You can add to the array.
<?php
$_POST['email'] = "email@Gmail.com";
$data = $_POST['email'];
if(preg_match("/\b(hotmail|gmail|yahoo)\b/i", $data)){
echo " Found free Email service.";
exit;
}
else{
echo "No match found for free Email service.";
exit;
}
Actually, you can use:
if(preg_match("/(hotmail|gmail|yahoo)/i", $data))
instead of:
if(preg_match("/\b(hotmail|gmail|yahoo)\b/i", $data))
which gave the same results.
回答2:
How about a white/black list of domains like the following:
$domainWhitelist = ['companydomain.org', 'companydomain.com'];
$domainBlacklist = ['gmail.com', 'hotmail.com'];
$domain = array_pop(explode('@', $email));
//white list
if(in_array($domain, $domainWhitelist)) {
//allowed
}
//black list
if(!in_array($domain, $domainBlacklist)) {
//allowed
}