2

i want to validate email addresses to make sure they are college edu addresses. What is the best way to do that? I have a list of colleges in a database and for each one I specify a url that should be part of a user's email address for that college. So for harvard, the url is "harvard.edu," because all of the harvard email addresses has that as part of their hostname.

Does anyone have a PHP function that will allow me to do that?

hakre
  • 184,866
  • 48
  • 414
  • 792
Chris Hansen
  • 6,293
  • 12
  • 67
  • 141
  • Possible duplicate of: [How to validate an Email in PHP?](http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php) – hakre Dec 18 '12 at 17:31

8 Answers8

1

Note: This will only check for *.edu email addresses

You could simplify it to just check for edu e-mails:

$email = "myemail@uni.edu";

function isEduEmail($email){
  $email = explode(".",$email);
  if($email[count($email)-1] == "edu")
    return true;
  else
    return false;
}

var_dump(isEduEmail($email)); // echo bool(true)

I realise for others that there would be a better solution using regex, but I'm rubbish at it so this a solution.

Prisoner
  • 26,783
  • 10
  • 71
  • 99
1

I would first check if the email is a valid email

if(filter_var($email, FILTER_VALIDATE_EMAIL))

if it is I would extract the domain name

$mailDomain = substr(strrchr($email, "@"), 1);

once it's extracted I would check if it exists in my base

mysql_query("SELECT count(domain_name) from domain where domain_name = ".mysql_real_escape_string($mailDomain));
Pierre Lacave
  • 2,500
  • 2
  • 18
  • 28
0

Checks if the string ends in .edu

<?php

$email = "myemail@uni.edu";

function isEduEmail($email){
  $eLength = strlen($email) - 1;
  $local = substr($email, $eLength - 3, $eLength );
  if($local === '.edu') return true;
  return false;
}

var_dump(isEduEmail($email)); // echo bool(true)

?>

DEMO: http://codepad.org/97LlxPX1

Naftali
  • 142,114
  • 39
  • 237
  • 299
  • this fail badly -- http://codepad.org/zv7btVJx country TLD is something have to take care too – ajreal Aug 24 '11 at 15:01
  • @ajreal -- I said it checks for `.edu` email addresses... not `uni.edu.sg` (which i have never heard of btw) – Naftali Aug 24 '11 at 15:02
  • is country TLD, between OP said he has the list of domain name to compare, should be use that for comparison? – ajreal Aug 24 '11 at 15:06
0

Since you have data in mysql, an example query like this should works

$some_email = ... ; // don't forget to sanitize
$sql = <<<SQL
select count(*) from tables
where domain_name = substring_index('{$some_email}', '@', -1);
SQL;

... // processing
// return >=1 is a valid edu email
ajreal
  • 45,869
  • 10
  • 83
  • 118
0

Single lined one:

function isEduEmail($sEmail) {
    return (substr($sEmail, strrpos($sEmail, ".")+1) == "edu");
}
Veger
  • 35,906
  • 11
  • 106
  • 114
Wesley van Opdorp
  • 14,720
  • 4
  • 40
  • 59
0

If you have a list of domains, just check whether given email address ends with "@whitelisted.domain". You can use filter_var() to validate whether it's a correct email address and then strpos() to check whether it contains the right domain.

Example:

$domains = array('test.org', 'fake.com');

$email = 'test@fake.com';
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
$email_ok = false;
foreach ($domains as $d) {
    if (strpos($email, '@' . $d) !== false) {
        $email_ok = true;
        break;
    }
}
// $email_ok is true if email's domain is on our whitelist
var_dump($email, $email_ok);

If you want to do the validation by querying MySQL server, extract the part of email after @ and check whether it's on your list.

piotrp
  • 3,565
  • 1
  • 22
  • 26
  • you are still not checking if it has a `.edu` email address – Naftali Aug 24 '11 at 15:01
  • _"for each one I specify a url that should be part of a user's email address for that college. So for harvard, the url is "harvard.edu"_ So `.edu` is part of the whitelisted address part (which is good, because not every college may have an address with .edu TLD). – piotrp Aug 24 '11 at 15:07
0

Something like this (hope you get the idea)

I'm not sure if this is valid code, because wrote without testing:

<?php

$usersEmail1 = "i.am@harvard.edu";

$email1Exploded = explode("@", $usersEmail1);
if (sizeof($email1Exploded) == 2) {
  $domain1 = trim($email1Exploded[1]);
  $result = mysql_query("SELECT * from valid_domains WHERE url LIKE '%" . mysql_real_escape_string($domain1) . "' LIMIT 1");
  if (!empty(mysql_num_rows($result))) {
    //Valid email
  } else {
    //Invalid email
  }
} else {
  //not valid email
}


?>
Janis Veinbergs
  • 6,977
  • 5
  • 46
  • 77
0
$email="bob@harvard.edu";
$lthree=substr($email,-3);
$edu=false;
if ($lthree=="edu") $edu=true;

Now if $edu is true, then the email is an edu email.

Adam F
  • 1,742
  • 1
  • 17
  • 18