Monday, June 11, 2012

PHP - Email Address Validation


PHP - Email Address Validation


Simple validation


$email = 'mail@example.com';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);


if ( $validation ) $output = 'proper email address';
else $output = 'wrong email address';


echo $output;


Advanced validation


<?php
$email="test@geemail.com";
if (isValidEmail($email))
{
       echo "Hooray! Adress is correct.";
}
else
{
       echo "Sorry! No way.";
}


//Check-Function
function isValidEmail($email)
{
       //Perform a basic syntax-Check
       //If this check fails, there's no need to continue
       if(!filter_var($email, FILTER_VALIDATE_EMAIL))
       {
               return false;
       }


       //extract host
       list($user, $host) = explode("@", $email);
       //check, if host is accessible
       if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
       {
               return false;
       }


       return true;
}
?>

No comments:

Post a Comment