正在注册的电子邮件的 PHP 检查域是“school.edu”地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6917198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Check domain of email being registered is a 'school.edu' address
提问by Phil
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.
假设学校网站是:school.edu。
I'm having trouble writing a php filter that checks that the submitted email address has the domain of "school.edu"
我在编写检查提交的电子邮件地址是否具有“school.edu”域的 php 过滤器时遇到问题
I'll use an example. Dude #1 has an email of [email protected] and Dude #2 has an email at [email protected]. I want to make sure that Dude 1 gets an error message, and Dude #2 has a successful registration.
我会用一个例子。Dude #1 的电子邮件是 [email protected],Dude #2 的电子邮件是 [email protected]。我想确保 Dude 1 收到错误消息,并且 Dude #2 已成功注册。
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.
这就是我要做的事情的要点。在不久的将来,该网站将允许另外两个地区学校注册:school2.edu 和 school3.edu。然后,我需要检查器根据域的小列表(可能是数组?)来检查电子邮件,以验证电子邮件是否属于列表中的域名。
回答by Wesley Murch
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
}
}
回答by Brian Gordon
You can use regex:
您可以使用正则表达式:
if(preg_match('/^\w+@school\.edu$/i', $source_string) > 0)
//valid
Now proceed to tear me apart in the comments because there's some crazy email address feature I didn't account for :)
现在继续在评论中撕裂我,因为我没有考虑到一些疯狂的电子邮件地址功能:)
回答by dougd_in_nc
Note that just getting everything after the @ may not accomplish what you are trying to accomplishbecause of email addresses like [email protected]. The get_domain function below will only get the domain down to the second level domain. It will return "unc.edu" for [email protected] or [email protected]. Also, you may want to account for domains with country codes (which have top level domains of 2 characters).
请注意,由于像 [email protected] 这样的电子邮件地址,仅在 @ 之后获取所有内容可能无法完成您要完成的任务。下面的 get_domain 函数只会将域降到二级域。它将为 [email protected] 或 [email protected] 返回“unc.edu”。 此外,您可能需要考虑带有国家/地区代码的域(具有 2 个字符的顶级域)。
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 = '[email protected]';
if (in_array(get_domain($email), $school_domains))
{
echo "good";
}
回答by Chris Hepner
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:
'whatever.edu' 部分将始终在@
. 所以,你需要做的就是:
- Find the last occurrence of
@
in the string. (In a normal email, there will only beone, but that doesn't matter here.) - Get the portion of the email after the
@
. This will be the domain name. - Use
in_array()
to compare the domain name against a list of accepted domains in$acceptedDomains
.
- 查找
@
字符串中最后出现的。(在普通电子邮件中,只会有一个,但这在这里无关紧要。) - 获取电子邮件后的部分
@
。这将是域名。 - 使用
in_array()
的域名反对接受域的列表进行比较$acceptedDomains
。
Note that if you want to also accept emails from [email protected]
, 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.
请注意,如果您还想接受来自 的电子邮件[email protected]
,则只需多做一点,但这可能适用也可能不适用于此处。我还假设您在执行此操作之前已验证电子邮件地址格式正确。
回答by Sampath Perera
Simple and Single line:
简单单行:
list($name, $domain) = explode('@', $email);
回答by ahinkle
If you are using Laravel:
如果您使用的是 Laravel:
Str::endsWith($email, '@foo.bar.edu'); // bool
回答by Patrick Perini
You could always do something like this:
你总是可以做这样的事情:
$good_domains = array('school.edu'); //in the future, just add to this array for more schools
$email = "[email protected]"; //your user's email
$matches = array();
preg_match("/^(.+)@([^\(\);:,<>]+\.[a-zA-Z]+)/", $email, &$matches); //validates the email and gathers information about it simultaneously
//should return ['[email protected]', 'user', 'mail.com']
$domain = $matches[3];
if(in_array($domain, $goood_domains))
{
//success! your user is from school.edu!
}
else
{
//oh no! an imposter!
}