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,小编认为这篇文章很具有学习的价值,分享给大家,希望对大家的学习有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
, 邮箱
验证
h1z1邮箱验证教程、h1z1邮箱验证教程视频、h1z1pc验证邮箱教程、h5 验证码页面示例、汉英翻译技巧示例 pdf,以便于您获取更多的相关知识。

时间: 2024-09-02 09:33:29

PHP邮箱验证示例教程_php技巧的相关文章

PHP开发环境配置(MySQL数据库安装图文教程)_php技巧

一. MySQL的安装 运行MYSQL安装程序(mysql-essential-5.1.40-win32.msi) 开发环境配置(MySQL数据库安装图文教程)_php技巧-mysql数据库主从配置">   选择安装类型为Custom   点选Change按钮更改安装目录   将安装目录更改为到D盘(可根据自己的系统更改)     点击Install按钮开始安装   安装程序将开始安装MySQL到指定的路径中     安装过程中汇出现一些广告点Next跳过即可.     安装完成后出现以下

phpmailer绑定邮箱的实现方法_php技巧

本文实例讲述了phpmailer绑定邮箱的实现方法.分享给大家供大家参考,具体如下: 效果如下: 1.配置 <?php return array ( 'email_host' => 'smtp.aliyun.com', 'email_port' => '25', 'email_username' => 'diandodo@aliyun.com', 'email_password' => 'xxxxxx', 'email_from' => 'diandodo@aliyun

使用XHGui来测试PHP性能的教程_php技巧

Profiling是一项用来观察程序性能的技术,非常适用于发现程序的瓶颈或者紧张的资源.Profiling能够深入程序的内部,展现request处理过程中每一部分代码的性能:同时,也可以确定有问题的请求(request):对于有问题的请求,我们还可以确定性能问题发生在请求内部的位置.对于PHP,我们有多种Profiling工具,本文主要集中在--XHGui,一款非常优秀的工具.XHGui构建在XHProf之上(XHProf由Facebook发布),但是对于剖析结果增加了更好的存储,同时增加了更加

php版微信小店调用api示例代码_php技巧

本文实例讲述了php版微信小店调用api的方法.分享给大家供大家参考,具体如下: 刚开始调用微信小店api的时候,可能大家会遇到问题.系统总是提示system error,归根结底还是发送的参数不正确. 下面给出几个调用例子: 例子写得不全. <?php function cUrlRequest($url,$data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CU

PHP实现的方程求解示例分析_php技巧

本文实例讲述了PHP实现的方程求解.分享给大家供大家参考,具体如下: 一.需求 1. 给出一个平均值X,反过来求出来,得到这个平均值X的三个数X1 ,X2, X3,最大值与最小值的差值要小于0.4(X1-X3都是保留1位小数的数) 2. 这三个数X1, X2, X3代表了三组数.满足下面的公式: X1 = [(m1 - m2)/(m1 - m0) ] * 100 (@1); m0, m1, m2三个数的边界条件如下: 1)48<m0<51 2)0.45<m1 - m1<0.55 3

php格式化json函数示例代码_php技巧

本文讲述了php格式化json函数的示例代码.分享给大家供大家参考,具体如下: <?php $arr = array("ret"=>0,"data"=>array('a' => 1, 'b' => '2', 'c' => 3, 'd' => 4, 'e' => 5)); $json = json_encode($arr); /** * Formats a JSON string for pretty printing

PHP中的多种加密技术及代码示例解析_php技巧

对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法.有时又叫传统密码算法,就是加密密钥能够从解密密钥中推算出来,同时解密密钥也可以 从加密密钥中推算出来.而在大多数的对称算法中,加密密钥和解密密钥是相同的,所以也称这种加密算法为秘密密钥算法或单密钥算法. 信息加密技术的分类 单项散列加密技术(不可逆的加密) 属于摘要算法,不是一种加密算法,作用是把任意长的输入字符串变化成固定长的输出串的一种函数 MD5 string md5 ( string $str [, bool $raw_outp

php中使用sftp教程_php技巧

<?php /** php 中的sftp 使用教程 Telnet.FTP.SSH.SFTP.SSL (一) ftp 协议简介 FTP(File Transfer Protocol,文件传输协议)是互联网上常用的协议之一,人们用FTP实现互连网上的文件传输. 如同其他的很多通讯协议,FTP通讯协议也采用客户机 / 服务器(Client / Server )架构.用户可以通过各种不同的FTP客户端程序, 借助FTP协议,来连接FTP服务器,以上传或者下载文件FTP的命令传输和数据传输是通过不同的端口

PHP的Yii框架中Model模型的学习教程_php技巧

模型是 MVC 模式中的一部分, 是代表业务数据.规则和逻辑的对象. 模型是 CModel 或其子类的实例.模型用于保持数据以及与其相关的业务逻辑. 模型是单独的数据对象.它可以是数据表中的一行,或者一个用户输入的表单. 数据对象的每个字段对应模型中的一个属性.每个属性有一个标签(label), 并且可以通过一系列规则进行验证. Yii 实现了两种类型的模型:表单模型和 Active Record.二者均继承于相同的基类 CModel. 表单模型是 CFormModel 的实例.表单模型用于保持