PHP邮箱验证示例教程
作者:Voja Janjic 发布时间:2023-06-20 12:01:47
在用户注册中最常见的安全验证之一就是邮箱验证。根据行业的一般做法,进行邮箱验证是避免潜在的安全隐患一种非常重要的做法,现在就让我们来讨论一下这些最佳实践,来看看如何在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,小编认为这篇文章很具有学习的价值,分享给大家,希望对大家的学习有所帮助。


猜你喜欢
- 如下所示:# the basic ways = 0for x in range(10): s += x# the right ways =
- 前言Hello!大家好,有好几天没有跟大家见面咯~不知道大家是否在等待《小玩意儿》专栏的更新呢上一篇的文章【老师见打系列】:我只是写了一个自
- 一、中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,
- 项目需求:用户注册页面注册之后,系统会发送一封邮件到用户邮箱,用户点击链接以激活账户,其中链接中的用户信息需要加密处理一下其中激活自己邮箱的
- 1 InnoDB页的概念InnoDB是一个将表中的数据存储在磁盘上的存储引擎,即使我们关闭并重启服务器,数据还是存在。而真正处理数据的过程发
- 目录一.前提二.token加密与解密三.视图CBV四.framework认证功能五.利用postman软件在前端提交一.前提首先是这个代码基
- 一、什么是星号变量最初,星号变量是用在函数的参数传递上的,在下面的实例中,单个星号代表这个位置接收任意多个非关键字参数,在函数的*b位置上将
- 本文实例讲述了php基于websocket搭建简易聊天室实践。分享给大家供大家参考。具体如下:1、前言公司游戏里面有个简单的聊天室,了解了之
- 技术栈vue.js 主框架vuex 状态管理vue-router 路由管理一般过程在一般的登录过程中,一种前端方案是:检查状态:进入页面时或
- 摘要在这篇文章里,我将以反模式的角度来直接讨论Django的低级ORM查询方法的使用。作为一种替代方式,我们需要在包含业务逻辑的
- Django自带强大的User系统,为我们提供用户认证、权限、组等一系列功能,可以快速建立一个完整的后台功能。但User模型并不能满足我们的
- 元组:# 元组,一种不可变的序列,在创建之后不能做任何的修改# 1.不可变# 2.用()创建元组类型,数据项用逗号来分割# 3.可以是任何的
- 三种方法:①直接使用dict②使用defaultdict③使用Counter ps:`int()`函数默认返回0 ①di
- 如果有人问你,GET和POST,有什么区别?你会如何回答?真实案例 前几天有人问我这个问题。
- 概述Object.freeze(obj)可以冻结一个对象。一个被冻结的对象再也不能被修改;冻结了一个对象则不能向这个对象添加新的属性,不能删
- 正则表达式的介绍1)在实际开发过程中经常会有查找符合某些复杂规则的字符串的需要,比如:邮箱、手机号码等,这时候想匹配或者查找符合某些规则的字
- 使用Python实现Word文档的自动化处理,包括批量生成Word文档、在Word文档中批量进行查找和替换、将Word文档批量转换成PDF等
- 一段重用很高的ajax代码,可以套用 <!DOCTYPE HTML
- 在matplotlib中,errorbar方法用于绘制带误差线的折线图,基本用法如下plt.errorbar(x=[1, 2, 3, 4],
- 数据存储·在javascript中,数据存储的位置会对代码整体性能产生重大的影响。·数据存储共有4种方式:字面量、变量、数组、对象成员。·要