小编给大家分享一下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 > 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.');
以上是“PHP邮箱验证的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。