Welcome Guest, Not a member yet? Register   Sign In
email_helper.php valid_email replacement function
#1

[eluser]jblack199[/eluser]
As the title states this is simply a replacement function for the valid_email function inside the helper file: email_helper.php. I actually wrote this function before I used CI when I was using smarty to truly check if the submitted email address was indeed plausible.

Most valid_email functions only check that the structure is [email protected] and this can leave you open to rogue registrations, requests, etc from bots. Have you noticed many bots use fake domain names? Well why not account for that in the code too??

Code:
if (! function_exists('valid_email'))
{
    function valid_email($address)
    {
        $isValid = 'true';
        $atIndex = strrpos($address, "@");
        if (is_bool($atIndex) && !$atIndex) {
            $isValid = 'false';
        } else {
            $domain = substr($address, $atIndex+1);
            $local = substr($address, 0, $atIndex);
            $localLen = strlen($local);
            $domainLen = strlen($domain);
            if ($localLen < 1 || $localLen > 64) {
                $isValid = 'false';
            } elseif($domainLen < 1 || $domainLen > 255) {
                $isValid = 'false';
            } elseif ($local[0] == '.' || $local[$localLen-1] == '.') {
                $isValid = 'false';
            } elseif(preg_match('/\\.\\./', $local)) {
                $isValid = 'false';
            } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+%/', $domain)) {
                $isValid = 'false';
            } elseif(preg_match('/\\.\\./', $domain)) {
                $isValid = 'false';
            } elseif(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
                if(!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) {
                    $isValid = 'false';
                }
            }
            if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
                $isValid = 'false';
            }
        }
        return $isValid;
    }
}

This functions does several things. The main things being it checks the length of the local string before @ and it checks the length of the string after the @. it also checks the entire configuration of the email to ensure it truly is like: [email protected] and last but not least, it checks the dns registries for the MX key and the A record which tells the script that it is in fact a legal registered domain which then tells you its plausible to have [email protected] as a valid email address.

Like I said I've used this function a while, hopefully someone else can find use for it as well.




Theme © iAndrew 2016 - Forum software by © MyBB