Arijit Aich
Arijit Aich

Reputation: 161

How to restrict public email id for registration in PHP?

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.

Upvotes: 0

Views: 3026

Answers (2)

vim
vim

Reputation: 1550

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
}

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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 protected]";
$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.

Upvotes: 0

Related Questions