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.
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 boundaryi
- case insensitive
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.
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
}
来源:https://stackoverflow.com/questions/28810526/how-to-restrict-public-email-id-for-registration-in-php