How to restrict public email id for registration in PHP?

一曲冷凌霜 提交于 2019-12-01 01:45:46

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

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!