PHP邮箱验证示例教程,php邮箱示例
PHP邮箱验证示例教程,php邮箱示例
在用户注册中最常见的安全验证之一就是邮箱验证。根据行业的一般做法,进行邮箱验证是避免潜在的安全隐患一种非常重要的做法,现在就让我们来讨论一下这些最佳实践,来看看如何在PHP中创建一个邮箱验证。
让我们先从一个注册表单开始:
<form method="post" action="http://mydomain.com/registration/"> <fieldset class="form-group"> <label for="fname">First Name:</label> <input type="text" name="fname" class="form-control" required /> </fieldset> <fieldset class="form-group"> <label for="lname">Last Name:</label> <input type="text" name="lname" class="form-control" required /> </fieldset> <fieldset class="form-group"> <label for="email">Last name:</label> <input type="email" name="email" class="form-control" required /> </fieldset> <fieldset class="form-group"> <label for="password">Password:</label> <input type="password" name="password" class="form-control" required /> </fieldset> <fieldset class="form-group"> <label for="cpassword">Confirm Password:</label> <input type="password" name="cpassword" class="form-control" required /> </fieldset> <fieldset> <button type="submit" class="btn">Register</button> </fieldset> </form>
接下来是数据库的表结构:
CREATE TABLE IF NOT EXISTS `user` ( `id` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, `fname` VARCHAR(255) , `lname` VARCHAR(255) , `email` VARCHAR(50) , `password` VARCHAR(50) , `is_active` INT(1) DEFAULT '0', `verify_token` VARCHAR(255) , `created_at` TIMESTAMP, `updated_at` TIMESTAMP, );
一旦这个表单被提交了,我们就需要验证用户的输入并且创建一个新用户:
// Validation rules $rules = array( 'fname' => 'required|max:255', 'lname' => 'required|max:255', 'email' => 'required', 'password' => 'required|min:6|max:20', 'cpassword' => 'same:password' ); $validator = Validator::make(Input::all(), $rules); // If input not valid, go back to registration page if($validator->fails()) { return Redirect::to('registration')->with('error', $validator->messages()->first())->withInput(); } $user = new User(); $user->fname = Input::get('fname'); $user->lname = Input::get('lname'); $user->password = Input::get('password'); // You will generate the verification code here and save it to the database // Save user to the database if(!$user->save()) { // If unable to write to database for any reason, show the error return Redirect::to('registration')->with('error', 'Unable to write to database at this time. Please try again later.')->withInput(); } // User is created and saved to database // Verification e-mail will be sent here // Go back to registration page and show the success message return Redirect::to('registration')->with('success', 'You have successfully created an account. The verification link has been sent to e-mail address you have provided. Please click on that link to activate your account.');
注册之后,用户的账户仍然是无效的直到用户的邮箱被验证。此功能确认用户是输入电子邮件地址的所有者,并有助于防止垃圾邮件以及未经授权的电子邮件使用和信息泄露。
整个流程是非常简单的——当一个新用户被创建时,在注册过过程中,一封包含验证链接的邮件便会被发送到用户填写的邮箱地址中。在用户点击邮箱验证链接和确认邮箱地址之前,用户是不能进行登录和使用网站应用的。
关于验证的链接有几件事情是需要注意的。验证的链接需要包含一个随机生成的token,这个token应该足够长并且只在一段时间段内是有效的,这样做的方法是为了防止网络攻击。同时,邮箱验证中也需要包含用户的唯一标识,这样就可以避免那些攻击多用户的潜在危险。
现在让我们来看看在实践中如何生成一个验证链接:
// We will generate a random 32 alphanumeric string // It is almost impossible to brute-force this key space $code = str_random(32); $user->confirmation_code = $code;
一旦这个验证被创建就把他存储到数据库中,发送给用户:
Mail::send('emails.email-confirmation', array('code' => $code, 'id' => $user->id), function($message) { $message->from('my@domain.com', 'Mydomain.com')->to($user->email, $user->fname . ' ' . $user->lname)->subject('Mydomain.com: E-mail confirmation'); });
邮箱验证的内容:
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8" /> </head> <body> <p style="margin:0"> Please confirm your e-mail address by clicking the following link: <a href="http://mydomain.com/verify?code=<?php echo $code; ?>&user=<?php echo $id; ?>"></a> </p> </body> </html>
现在让我们来验证一下它是否可行:
$user = User::where('id', '=', Input::get('user')) ->where('is_active', '=', 0) ->where('verify_token', '=', Input::get('code')) ->where('created_at', '>=', time() - (86400 * 2)) ->first(); if($user) { $user->verify_token = null; $user->is_active = 1; if(!$user->save()) { // If unable to write to database for any reason, show the error return Redirect::to('verify')->with('error', 'Unable to connect to database at this time. Please try again later.'); } // Show the success message return Redirect::to('verify')->with('success', 'You account is now active. Thank you.'); } // Code not valid, show error message return Redirect::to('verify')->with('error', 'Verification code not valid.');
结论:
上面展示的代码只是一个教程示例,并且没有通过足够的测试。在你的web应用中使用的时候请先测试一下。上面的代码是在Laravel框架中完成的,但是你可以很轻松的把它迁移到其他的PHP框架中。同时,验证链接的有效时间为48小时,之后就过期。引入一个工作队列就可以很好的及时处理那些已经过期的验证链接。
本文实PHPChina原创翻译,原文转载于http://www.phpchina.com/portal.php?mod=view&aid=39888,小编认为这篇文章很具有学习的价值,分享给大家,希望对大家的学习有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

OKX 欧易是一个领先的加密货币交易平台。本文将提供详细的步骤,指导您如何注册 OKX 欧易官方网站账户。您将了解如何访问官方网站、选择注册方式、填写必要信息并完成注册过程。文中还包含有关注意事项的信息,例如使用真实个人信息并设置强密码的重要性。

详细介绍芝麻开门交易所网页版登入口操作,含登录步骤、找回密码流程,还针对登录失败、无法打开页面、收不到验证码等常见问题提供解决方法,助你顺利登录平台。

Gateio 交易所 app 老版本下载渠道,涵盖官方、第三方应用市场、论坛社区等途径,还给出下载注意事项,帮你轻松获取老版本,解决新版本使用不适或设备兼容问题。

本指南提供了 Bitget 交易所官方 App 的详细下载和安装步骤,适用于安卓和 iOS 系统。指南整合了来自多个权威来源的信息,包括官网、App Store 和 Google Play,并强调了下载和账户管理过程中的注意事项。用户可以从官方渠道下载 App,包括应用商店、官网 APK 下载和官网跳转,并完成注册、身份验证和安全设置。此外,指南还涵盖了常见问题和注意事项,例如

Gate.io(芝麻开门)是全球领先的加密货币交易平台,本文提供Gate.io现货交易完整教程。教程涵盖账户注册登录、KYC认证、法币及数字货币充值、交易对选择、限价/市价交易下单以及订单和交易记录查看等步骤,助您快速上手Gate.io平台进行加密货币交易。 无论是新手还是老手,都能从本教程中获益,轻松掌握Gate.io交易技巧。

芝麻开门交易平台登录方式便捷,用户只需访问其官方网站(请自行搜索域名),输入注册邮箱/手机号及密码即可登录。平台可能启用2FA等安全验证机制,保障账户安全。

数字货币合约交易:高收益与潜在风险并存的投资策略数字货币合约交易,与现货交易不同,投资者需预测数字货币价格的涨跌趋势,选择做多或做空合约来获利。合约交易通常使用杠杆,潜在收益高于现货交易,但也伴随着更高的风险。本文将揭示数字货币合约交易中常见的陷阱,并提供在欧易OKX交易所进行合约交易的详细步骤。数字货币合约交易的风险与陷阱数字货币合约市场暗藏诸多风险,不法分子或平台可能利用规则漏洞牟利。常见的陷阱包括:价格操纵:通过集中买卖操纵市场价格,人为抬高或压低价格以获利。信息不对称:平台或交易

OKX交易平台,备受全球用户信赖的数字资产交易平台。平台提供多种数字资产交易服务,包括现货交易、期货交易、期权交易等。
