Take the 2-minute tour ×

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.

share|improve this question
And what have you come up with? Have you started writing anything at all, or researching php's regular expressions, for instance? Also what about academic registrants from other countries, such as the UK (with the .ac.uk tld)? –  David Thomas Aug 2 '11 at 19:07
mark@fb.com - he might have some pointers for you...or maybe you could get ahold of one of the winklevi, they have some experience here. –  Jim Rubenstein Aug 2 '11 at 19:23
Right now its just for some local schools. We're almost done coding the whole site. Just adding some final features, such as the email validation script. –  Phil Aug 2 '11 at 19:27
And Jim, as much as I'd love to pick Mark's brain on coding and stuff, I doubt he has his email publicly available. Sadly. –  Phil Aug 2 '11 at 19:34
Phil, use @jim to let him know he's been mentioned –  Brian Gordon Aug 2 '11 at 19:45

5 Answers 5

There are surely several ways to check, here is what I've come up with off the top of my head:

$email = $_POST['email'];
$allowed = array('school.edu', 'school2.edu', 'school3.edu');

// Make sure the address is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL))
    $domain = array_pop(explode('@', $email));

    if ( ! in_array($domain, $allowed))
        // Not allowed


explode() separates the string by @ characters (there should be only one anyways, so maybe a substr() would be slightly more efficient), and array_pop() removes and returns the last one, which should be everything after the @ and be the domain of the email.

There may be a much better or more concise method, but this should work regardless.

share|improve this answer
Just as an added suggestion, and this probably for all text inputs, remember to run trim() on your submitted values, in case someone adds a space at the end of the email address –  thorne51 Dec 15 at 7:20

Note that just getting everything after the @ may not accomplish what you are trying to accomplish because of email addresses like user@students.ecu.edu. The get_domain function below will only get the domain down to the second level domain. It will return "unc.edu" for username@unc.edu or username@mail.unc.edu. Also, you may want to account for domains with country codes (which have top level domains of 2 characters).

You can use the function below to extract the domain. Then you can use an array of school domains, or put the school domains in a database and check the email address against that.

    function get_domain($email)
       if (strrpos($email, '.') == strlen($email) - 3)
          $num_parts = 3;
          $num_parts = 2;

       $domain = implode('.',
            array_slice( preg_split("/(\.|@)/", $email), - $num_parts)

        return strtolower($domain);

    // test the function
    $school_domains = array('unc.edu', 'ecu.edu');

    $email = 'someone@students.ecu.edu';

    if (in_array(get_domain($email), $school_domains))
        echo "good";
share|improve this answer

You can use regex:

if(preg_match('/^\w+@school\.edu$/i', $source_string) > 0)

Now proceed to tear me apart in the comments because there's some crazy email address feature I didn't account for :)

share|improve this answer

You could always do something like this:

$good_domains = array('school.edu'); //in the future, just add to this array for more schools

$email = "user@school.edu"; //your user's email

$matches = array();
preg_match("/^(.+)@([^\(\);:,<>]+\.[a-zA-Z]{2,4})/", $email, &$matches); //validates the email and gathers information about it simultaneously
//should return ['user@mail.com', 'user', 'mail.com']
$domain = $matches[3];

if(in_array($domain, $goood_domains))
    //success! your user is from school.edu!
    //oh no! an imposter!
share|improve this answer

I'd just do this:

 $acceptedDomains = array('site1.edu', 'site2.edu');

 if(in_array(substr($email, strrpos($email, '@') + 1), $acceptedDomains))
    // Email is from a domain in $acceptedDomains

The 'whatever.edu' portion will always be after the @. So, all you need to do is:

  1. Find the last occurrence of @ in the string. (In a normal email, there will only be one, but that doesn't matter here.)
  2. Get the portion of the email after the @. This will be the domain name.
  3. Use in_array() to compare the domain name against a list of accepted domains in $acceptedDomains.

Note that if you want to also accept emails from email@any.subdomain.site1.edu, you'd have to do just a little more, but that may or may not be applicable here. I'm also assuming you've validated that the email addresses are well formed before doing this.

share|improve this answer

Your Answer


