QuickForm - E-Mail validation with checkdnsrr() - subdomains

From: Date: Wed, 10 Feb 2010 13:47:02 +0000
Subject: QuickForm - E-Mail validation with checkdnsrr() - subdomains
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-30272@lists.php.net to get a copy of this message
Hello This is actually not specifically a QuickForm question, but it is related to this function I took from the QuickForm manual, and which is actually built-in natively in QuickForm, too: function checkEmail($email, $domainCheck = false) {
    if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'.
                   '\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) {
        if ($domainCheck && function_exists('checkdnsrr')) {
            list (, $domain)  = explode('@', $email);
            if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
                return true;
            }
            return false;
        }
        return true;
    }
    return false;
} Now it happend that the check for an e-mail address of the following form returned false, although the address was claimed to be correct: firstname.lastname@subdomain.maindomain.tld So, both MX and A checks for subdomain.maindomain.tld returned false. Performing the checks for maindomain.tld only, both returned true. So I added a fix: function checkEmail($email, $domainCheck = false) {
    if (preg_match('/^[a-zA-Z0-9\._-]+\@(\[?)[a-zA-Z0-9\-\.]+'.
                   '\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $email)) {
        if ($domainCheck && function_exists('checkdnsrr')) {
            list (, $domain)  = explode('@', $email);
            if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
                return true;
            }
            // Fix: check with removed sub domains
            $spl = explode(".", $domain);
            $c = count($spl);
            $domain = $spl[$c - 2] . "." . $spl[$c - 1];
            if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
                return true;
            }
            return false;
        }
        return true;
    }
    return false;
} Now, I am not familiar with anything about MX and A records. Is this a sensible fix, which should even make it into QuickForm(2)? Or is it rather likely to introduce new problems I am not aware of? Thanks for your comments.

« previous php.pear.general (#30272) next »