当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP邮箱验证示例教程

PHP邮箱验证示例教程

2017年12月12日  | 移动技术网IT编程  | 我要评论

硬汉2迅雷下载,抚顺吧,唐人美国十次啦

在用户注册中最常见的安全验证之一就是邮箱验证。根据行业的一般做法,进行邮箱验证是避免潜在的安全隐患一种非常重要的做法,现在就让我们来讨论一下这些最佳实践,来看看如何在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原创翻译,原文转载于,小编认为这篇文章很具有学习的价值,分享给大家,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网