PHP检查注册的电子邮件地址是一个'school.edu'地址 [英] PHP Check domain of email being registered is a 'school.edu' address

查看:171
本文介绍了PHP检查注册的电子邮件地址是一个'school.edu'地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我正在努力工作的项目写一个功能,我们正在为一个机构的学生,工作人员和校友提供一个网站。



我们假设学校的网站是:school.edu。



我无法编写一个php过滤器,检查提交的电子邮件地址是否具有域的school.edu



我会用一个例子。 Dude#1的电子邮件为user@mail.com,Dude#2的电子邮件地址为user@school.edu。我想确保Dude 1得到一个错误消息,Dude#2已成功注册。



这是我想要做的事情。在不久的将来,该网站将允许另外两个地方学校注册:school2.edu和school3.edu。然后,我需要检查器根据一个小列表(可能是一个数组)来检查邮件,以确认邮件是列表中的域名。

解决方案

肯定有几种方法来检查,这里是我从头顶上出来的:

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

//确保该地址有效
如果(filter_var($ email,FILTER_VALIDATE_EMAIL))
{
$ explosEmail = explode('@',$ email );
$ domain = array_pop($ expandingEmail);

如果(!in_array($ domain,$ allowed))
{
//不允许
}

}

explode()将字符串与 @ 字符(应该只有一个,所以也许一个 substr()将稍微更有效),而$ code> array_pop()删除并返回最后一个,应该是 @ 之后的所有内容,并且是电子邮件的域。 >

可能有一个更好或更简洁的方法,但这应该是无关紧要的。


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.

解决方案

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))
{
    $explodedEmail = explode('@', $email);
    $domain = array_pop($explodedEmail);

    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.

这篇关于PHP检查注册的电子邮件地址是一个'school.edu'地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆