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

后端 未结 7 1134
心在旅途
心在旅途 2020-12-10 02:59

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

相关标签:
7条回答
  • 2020-12-10 03:31

    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;
           else
              $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";
        }
    
    0 讨论(0)
提交回复
热议问题