Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

validation - PHP Check domain of email being registered is a 'school.edu' address

I need to write a function for a project i'm working on for fun, where we're making a site only accessible to students, staff, and alumni at an institution.

Let's say the schools website is: school.edu.

I'm having trouble writing a php filter that checks that the submitted email address has the domain of "school.edu"

I'll use an example. Dude #1 has an email of user@mail.com and Dude #2 has an email at user@school.edu. I want to make sure that Dude 1 gets an error message, and Dude #2 has a successful registration.

That's the gist of what I'm trying to do. In the near future the site will allow registration by another two locale schools: school2.edu and school3.edu. I will then need the checker to check the email against a small list (maybe an array?) of domains to verify that the email is of a domain name on the list.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a few ways to accomplish this, here's one:

// Make sure we have input
// Remove extra white space if we do
$email = isset($_POST['email']) ? trim($_POST['email']) : null;

// List of allowed domains
$allowed = [
    'school.edu',
    'school2.edu',
    'school3.edu'
];

// Make sure the address is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
    // Separate string by @ characters (there should be only one)
    $parts = explode('@', $email);

    // Remove and return the last part, which should be the domain
    $domain = array_pop($parts);

    // Check if the domain is in our list
    if ( ! in_array($domain, $allowed))
    {
        // Not allowed
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...