Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
如何通过验证令牌在 PHP 中设置电子邮件验证:完整指南_创想鸟

如何通过验证令牌在 PHP 中设置电子邮件验证:完整指南

如何通过验证令牌在 php 中设置电子邮件验证:完整指南

电子邮件验证是确保电子邮件地址存在并且可以接收电子邮件的过程。鉴于,电子邮件验证会检查地址格式是否正确;也就是说 – 根据特定标准(例如 utf-8)编写。 

在本文中,我将讨论 php 电子邮件验证以及如何将其用于 web 开发和通过验证令牌进行用户身份验证。文章涉及一些微教程,包括:

带有 mailtrap 的 phpmailer 配置

一个简单的 html 表单创建

立即学习“PHP免费学习笔记(深入)”;

基本电子邮件地址验证

生成令牌和凭证并将其存储在 sql 数据库中

使用验证令牌发送验证电子邮件

与验证相关的电子邮件测试

所以,让我们开始吧。 

设置电子邮件发送

要发送验证电子邮件,您可以使用 php 内置的 mail() 函数或像 phpmailer 这样的库,它提供更多功能和更好的可靠性。

由于我想让本教程尽可能安全且可用于生产,因此我将使用“phpmailer”。检查通过composer安装phpmailer的代码:

作曲家需要 phpmailer/phpmailer

为什么使用 mailtrap api/smtp?

这是一个电子邮件传送平台,可在一个地方测试、发送和控制您的电子邮件。而且,除其他外,您还可以获得以下内容:

各种语言的现成配置设置,包括 php 和 laravel。

smtp 和 api 以及主要语言的 sdk,包括 ofc、php。 

行业最佳的分析。 

27/7 人力支持,以及紧急案件的快速通道程序。 

所有这些都可以让您启动电子邮件验证过程,并确保所有人的安全和稳定。

继续使用 mailtrap 配置 phpmailer 的设置:

$phpmailer = new phpmailer();$phpmailer->issmtp();$phpmailer->host = 'live.smtp.mailtrap.io';$phpmailer->smtpauth = true;$phpmailer->port = 587;$phpmailer->username = 'api';$phpmailer->password = 'your_mailtrap_password';

这是 phpmailer 设置:

use phpmailerphpmailerphpmailer;use phpmailerphpmailerexception;require 'vendor/autoload.php';function sendverificationemail($email, $verificationcode) {    $mail = new phpmailer(true);    try {        // server settings        $mail->issmtp();        $mail->host = 'live.smtp.mailtrap.io';        $mail->smtpauth = true;        $mail->username = 'api';        $mail->password = 'your_mailtrap_password';        $mail->smtpsecure = phpmailer::encryption_starttls;        $mail->port = 587;        // recipients        $mail->setfrom('youremail@example.com', 'your website');        $mail->addaddress($email);        // content        $mail->ishtml(false);        $mail->subject = 'email verification';        $mail->body    = "your verification code is: $verificationcode";        $mail->send();        return true;    } catch (exception $e) {        return false;    }}

请注意,上面的代码不会发送验证令牌(点击此处跳转到带有验证令牌的代码片段)。这只是如何设置 mailtrap smtp 和定义验证功能的示例。以下是要点的快速细分:

phpmailer 和 exception 类被导入。

sendverificationemail($email, $verificationcode) 是函数定义。 

创建了一个新的 phpmailer 对象。 

try-catch 块处理电子邮件发送期间的异常。

服务器设置按照示例配置设置为 mailtrap。 

电子邮件内容设置为 ishtml(false) 为纯文本。 

提示:

电子邮件内容可以重构为 html。 

由于吞吐量限制,您应该避免使用 gmail.com 作为注册表单 smtp 中继。但如果您确实想创建邮件程序 php 文件并通过 gmail 发送,请查看本教程。 

创建注册表

下面是一个简单的注册表单,它包含标题和用户帐户信息(用户名、电子邮件和密码)。 

它没有任何 css 样式表或 div 类,因为这只是一个示例。

但是,我建议您将这些包含在生产中,并使它们与您品牌的设计语言保持一致。否则,您的表单可能看起来不专业,用户也不愿意参与其中。

 

    register                            


额外专业提示 – 考虑在表单中使用 javascript

如果您想要有关如何创建包含 recaptcha 的 php 联系表单的完整教程,请观看下面的视频⬇️。 

js 可以实时验证用户输入,对错误提供即时反馈,无需重新加载页面。 

通过在客户端捕获错误,js 可以减少发送到服务器的无效请求的数量,从而减少服务器负载并提高每个会话的性能。

使用ajax,js可以从服务器发送和接收数据,无需重新加载页面,提供更流畅的用户体验。

现在,我将转向电子邮件地址验证。  

电子邮件地址验证

这是一个检查域和 mx 记录的简单脚本。它基本上允许您通过执行 mx 查找来验证电子邮件。

<?php// this method checks if the domain part of the email address has a functioning mail server.$email = "user@example.com";list($user, $domain) = explode(separator:"@", $email)if (filter_var($email, filter:filter_validate_email) && getmxrr($domain, &hosts: $mxhosts)){    echo "valid email address with a valid mail server" . php_eol;} else {    echo "invalid email address or no valid mail server found" . php_eol;}

但是,该脚本不会发送电子邮件以进行用户激活和身份验证。此外,它不在 mysql 中存储任何数据。 

为此,我将在接下来的部分中执行以下操作:

生成验证令牌

创建 php mysql 架构来存储注册表单中的凭据

发送带有令牌的验证电子邮件

验证验证令牌

提示:类似的逻辑可以应用于注销/登录表单。

生成验证令牌

验证令牌是在注册过程中为每个用户生成的唯一字符串。该令牌包含在验证电子邮件中,有两种方法可以生成它。

方法1

第一种方法利用 bin2hex 命令创建一个随机令牌,参数设置为 (random_bytes(50))。

 

$token = bin2hex(random_bytes(50));

方法2

或者,您可以使用下面的脚本生成令牌。我将在电子邮件发送脚本中使用该脚本。

<?phpfunction generateverificationcode($length = 6) {    $characters = '0123456789';    $code = '';    for ($i = 0; $i 

存储验证令牌

在发送验证电子邮件之前,确保正确处理和存储用户数据至关重要。我将使用简单的 sql 架构来创建用户表,并将生成的令牌与用户的注册信息一起存储在数据库中。

create table users (    id int auto_increment primary key,    username varchar(50) not null,    email varchar(100) not null,    password varchar(255) not null,    token varchar(255) default null,    is_verified tinyint(1) default 0);

快速细分:

上面的脚本创建了一个包含以下列的用户表:

id – 每个用户的唯一标识符,自动递增。

username – 用户的用户名;它不能为空。

email – 用户的电子邮件地址;它不能为空。

password – 用户的密码(散列);它不能为空。

token – 验证令牌,可以为空。

is_verified – 指示用户是否已验证的标志(0 表示未验证,1 表示已验证),默认值为 0。

发送验证令牌 

总的来说,下面的脚本是本文前面讨论的所有内容的合并,其设计目的是:

生成随机数字验证码。 

使用phpmailer发送验证邮件到指定邮箱。

配置电子邮件服务器设置。 

处理潜在的错误。 

提供邮件是否发送成功的反馈。

请注意,该脚本面向 mailtrap 用户,并且它利用 smtp 方法。

<?phprequire 'vendor/autoload.php';use phpmailerphpmailerphpmailer;use phpmailerphpmailersmtpuse phpmailerphpmailerexception;//function to generate a random verification code1 usagefunction generateverificationcode($length = 6) {    $characters = '0123456789';    $code = '';    for ($i = 0; $i smtpdebug = smtp::debug_off; // set to debug_server for debugging        $mail ->issmtp();        $mail ->host = 'live.smtp.mailtrap.io'; // mailtrap smtp server host         $mail ->smtpauth = true;        $mail ->username = 'api'; // your mailtrap smtp username        $mail ->password = 'your_mailtrap_password'; // your mailtrap smtp password        $mail ->smtpsecure = phpmailer::encryption_starttls; // enable tls encryption        $email ->port = 587; // tcp port to connect to        //recipients        $mail->setfrom(address:'mailtrapclub@gmail.com', name:"john doe"); //sender's email and name        $mail->addaddress($email); // recipient's email        //content        $mail->ishtml(ishtml:false); //set to true if sending html email        $mail->subject = 'email verification';        $mail->body = "your verification code is: $verificationcode";        $mail->send();        return true;    }catch (exception $e) {        return false;    }}//example usage$email = "mailtrapclub+test@gmail.com"$verificationcode = generateverificationcode();if (sendverificationemail($email,$verificationcode)){    echo "a verification email has been sent to $email. please check your inbox and enter the code to verrify your email." . php_eol;} else {    echo "failed to send the verification email. please try again later." . php_eol;}

验证验证令牌

是的,标题有点循环,但这正是您所需要的。下面的脚本启用了“验证的验证”流程?,其移动方式如下:

用户点击验证链接。 令牌得到验证。用户的电子邮件在数据库中被标记为已验证。

connect_error) {    die("Connection failed: " . $conn->connect_error);}if (isset($_GET['token'])) {    $token = $_GET['token'];    $stmt = $conn->prepare("SELECT * FROM users WHERE token=? LIMIT 1");    $stmt->bind_param("s", $token);    $stmt->execute();    $result = $stmt->get_result();    if ($result->num_rows > 0) {        $user = $result->fetch_assoc();        $stmt->close();        $stmt = $conn->prepare("UPDATE users SET is_verified=1, token=NULL WHERE id=?");        $stmt->bind_param("i", $user['id']);        if ($stmt->execute() === TRUE) {            echo "Email verification successful!";        } else {            echo "Error: " . $conn->error;        }        $stmt->close();    } else {        echo "Invalid token!";    }}$conn->close();?>

我们感谢您选择这篇文章来了解更多有关 php 电子邮件验证的信息。要继续阅读文章并发现有关相关主题的更多文章,请关注 mailrap 博客!

以上就是如何通过验证令牌在 PHP 中设置电子邮件验证:完整指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1244414.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Symfony Station 公报 — 八月 看看 Symfony、Drupal、PHP、Cyber​​sec 和 Fediverse 新闻!
上一篇 2025年12月9日 16:49:38
使用自动加载翻译在 Laravel 中构建多态可翻译模型
下一篇 2025年12月9日 16:49:51

相关推荐

发表回复

登录后才能评论
关注微信