我需要为一个我正在从事的项目编写一个函数,目的是为了好玩,我们正在制作一个网站,仅供学校的学生、教职员工和校友访问.
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.
假设学校网站是:school.edu.
Let's say the schools website is: school.edu.
我在编写用于检查提交的电子邮件地址是否具有school.edu"域的 php 过滤器时遇到问题
I'm having trouble writing a php filter that checks that the submitted email address has the domain of "school.edu"
我会举一个例子.Dude #1 的电子邮件是 user@mail.com,Dude #2 的电子邮件是 user@school.edu.我想确保 Dude 1 收到错误消息,并且 Dude #2 已成功注册.
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.
这就是我想要做的事情的要点.在不久的将来,该网站将允许另外两个地区学校注册:school2.edu 和 school3.edu.然后,我需要检查器根据域的小列表(可能是数组?)来检查电子邮件,以验证电子邮件是否属于列表中的域名.
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'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
}
}
这篇关于正在注册的电子邮件的 PHP 检查域是“school.edu"地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!