正在注册的电子邮件的 PHP 检查域是“school.edu"地址

2023-09-23php开发问题
2

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

问题描述

我需要为一个我正在从事的项目编写一个函数,目的是为了好玩,我们正在制作一个网站,仅供学校的学生、教职员工和校友访问.

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"地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17