Best general and specific email authentication feature (college domain)?

Hey guys, I know that checking email is one of those things that isn't the funniest thing on the block. I am launching a website and I want to limit the audience only to people in my college, and I also want to get the preferred email address for my user. So this is a two-part question.

  • Is there really a reliable php function for email authentication?

  • Is it possible to check email from a specific domain. I do not want to just check if a domain exists, because I know that www.mycollege.edu already exists. Is it really possible to verify in any case that the user has a valid web address @ mycollege.edu? Thanks you!

+3
source share
7 answers

This is what I use:

   function check_email_address($email) {
        // First, we check that there one @ symbol, and that the lengths are right
        if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
            // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
            return false;
        }
        // Split it into sections to make life easier
        $email_array = explode("@", $email);
        $local_array = explode(".", $email_array[0]);
        for ($i = 0; $i < sizeof($local_array); $i++) {
            if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
                return false;
            }
        }
        if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
            $domain_array = explode(".", $email_array[1]);
            if (sizeof($domain_array) < 2) {
                return false; // Not enough parts to domain
            }
            for ($i = 0; $i < sizeof($domain_array); $i++) {
                if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
                    return false;
                }
            }
        }

        return true;
    }

EDIT Replaced deprecated ereg with preg_match to meet PHP 5.3 requirements

+22
source

If you really want to make sure that its validity makes your registration form, send them an email with a URL link in which they must click to confirm.

Thus, you not only know that the address is valid (because the letter was received), but you also know that the account owner has registered (if someone else does not know his login details).

To make sure it ends correctly, you can use explode () in the "@" field and check the second part.

$arr = explode('@', $email_address);
if ($arr[1] == 'mycollege.edu')
{
    // Then it from your college
}

PHP filter_var: http://www.w3schools.com/php/filter_validate_email.asp

+2

:

if (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])@mycollege.edu$/', $email)) {
     // Valid
}
0

http://ru2.php.net/manual/en/book.filter.php

var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
0

([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?

php preg_match

/([a-zA-Z0-9_-]+)(\@)([a-zA-Z0-9_-]+)(\.)([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?/i

@mycollege.edu

^([a-zA-Z0-9_-]+)(@mycollege.edu)$

php preg_match

/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i

PHP CODE

<?php 

$email = 'tahir_aS-adov@mycollege.edu';
preg_match('/^([a-zA-Z0-9_-]+)(@mycollege.edu)$/i', $email, $matches);

if ($matches) { 
    echo "Matched";
} else {
    echo "Not Matched";
}

var_dump($matches);
0

. , .

$email = "info@stakoverflow.com";

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
 echo $email ." is a valid email address";
} else {
  echo $email ." is not a valid email address";
}

, .

0

, filter_var php

<?php
function email_validation($email) {
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
       echo("$email is a valid email address");
    } else {
       echo("$email is not a valid email address");
    }
}

//Test
email_validation('johnson123');
?>
0

All Articles