开发者

college edu email addresses validation with php

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.

Doe开发者_Python百科s anyone have a PHP function that will allow me to do that?


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));


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.


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


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


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.


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
}


?>


Single lined one:

function isEduEmail($sEmail) {
    return (substr($sEmail, strrpos($sEmail, ".")+1) == "edu");
}


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜