PHP郵箱驗(yàn)證示例教程
在用戶注冊(cè)中最常見(jiàn)的安全驗(yàn)證之一就是郵箱驗(yàn)證。根據(jù)行業(yè)的一般做法,進(jìn)行郵箱驗(yàn)證是避免潛在的安全隱患一種非常重要的做法,現(xiàn)在就讓我們來(lái)討論一下這些最佳實(shí)踐,來(lái)看看如何在PHP中創(chuàng)建一個(gè)郵箱驗(yàn)證。
讓我們先從一個(gè)注冊(cè)表單開(kāi)始:
<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>
接下來(lái)是數(shù)據(jù)庫(kù)的表結(jié)構(gòu):
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, );
一旦這個(gè)表單被提交了,我們就需要驗(yàn)證用戶的輸入并且創(chuàng)建一個(gè)新用戶:
// 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.');
注冊(cè)之后,用戶的賬戶仍然是無(wú)效的直到用戶的郵箱被驗(yàn)證。此功能確認(rèn)用戶是輸入電子郵件地址的所有者,并有助于防止垃圾郵件以及未經(jīng)授權(quán)的電子郵件使用和信息泄露。
整個(gè)流程是非常簡(jiǎn)單的——當(dāng)一個(gè)新用戶被創(chuàng)建時(shí),在注冊(cè)過(guò)過(guò)程中,一封包含驗(yàn)證鏈接的郵件便會(huì)被發(fā)送到用戶填寫(xiě)的郵箱地址中。在用戶點(diǎn)擊郵箱驗(yàn)證鏈接和確認(rèn)郵箱地址之前,用戶是不能進(jìn)行登錄和使用網(wǎng)站應(yīng)用的。
關(guān)于驗(yàn)證的鏈接有幾件事情是需要注意的。驗(yàn)證的鏈接需要包含一個(gè)隨機(jī)生成的token,這個(gè)token應(yīng)該足夠長(zhǎng)并且只在一段時(shí)間段內(nèi)是有效的,這樣做的方法是為了防止網(wǎng)絡(luò)攻擊。同時(shí),郵箱驗(yàn)證中也需要包含用戶的唯一標(biāo)識(shí),這樣就可以避免那些攻擊多用戶的潛在危險(xiǎn)。
現(xiàn)在讓我們來(lái)看看在實(shí)踐中如何生成一個(gè)驗(yàn)證鏈接:
// 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;
一旦這個(gè)驗(yàn)證被創(chuàng)建就把他存儲(chǔ)到數(shù)據(jù)庫(kù)中,發(fā)送給用戶:
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'); });
郵箱驗(yàn)證的內(nèi)容:
<!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>
現(xiàn)在讓我們來(lái)驗(yàn)證一下它是否可行:
$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.');
結(jié)論:
上面展示的代碼只是一個(gè)教程示例,并且沒(méi)有通過(guò)足夠的測(cè)試。在你的web應(yīng)用中使用的時(shí)候請(qǐng)先測(cè)試一下。上面的代碼是在Laravel框架中完成的,但是你可以很輕松的把它遷移到其他的PHP框架中。同時(shí),驗(yàn)證鏈接的有效時(shí)間為48小時(shí),之后就過(guò)期。引入一個(gè)工作隊(duì)列就可以很好的及時(shí)處理那些已經(jīng)過(guò)期的驗(yàn)證鏈接。
本文實(shí)PHPChina原創(chuàng)翻譯,原文轉(zhuǎn)載于http://www.phpchina.com/portal.php?mod=view&aid=39888,小編認(rèn)為這篇文章很具有學(xué)習(xí)的價(jià)值,分享給大家,希望對(duì)大家的學(xué)習(xí)有所幫助。
- php實(shí)現(xiàn)驗(yàn)證郵箱格式的代碼實(shí)例
- PHP利用緩存處理用戶注冊(cè)時(shí)的郵箱驗(yàn)證,成功后用戶數(shù)據(jù)存入數(shù)據(jù)庫(kù)操作示例
- 實(shí)例講解PHP驗(yàn)證郵箱是否合格
- PHP自帶方法驗(yàn)證郵箱、URL、IP是否合法的函數(shù)
- PHP自帶方法驗(yàn)證郵箱是否存在
- php郵箱地址正則表達(dá)式驗(yàn)證
- php驗(yàn)證郵箱和ip地址最簡(jiǎn)單方法匯總
- js和php郵箱地址驗(yàn)證的實(shí)現(xiàn)方法
- php使用filter過(guò)濾器驗(yàn)證郵箱 ipv6地址 url驗(yàn)證
- PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
- 通過(guò)PHP實(shí)現(xiàn)用戶注冊(cè)后郵箱驗(yàn)證激活
相關(guān)文章
PHP中如何調(diào)用webservice的實(shí)例參考
本篇文章介紹了,PHP中如何調(diào)用webservice的實(shí)例參考。需要的朋友參考下2013-04-04javascript+php實(shí)現(xiàn)根據(jù)用戶時(shí)區(qū)顯示當(dāng)?shù)貢r(shí)間的方法
這篇文章主要介紹了javascript+php實(shí)現(xiàn)根據(jù)用戶時(shí)區(qū)顯示當(dāng)?shù)貢r(shí)間的方法,實(shí)例分析javascript獲取客戶端時(shí)區(qū)及與服務(wù)器端php交互的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php通過(guò)兩層過(guò)濾獲取留言內(nèi)容的方法
這篇文章主要介紹了php通過(guò)兩層過(guò)濾獲取留言內(nèi)容的方法,涉及php正則匹配及數(shù)組與字符串的相關(guān)操作技巧,需要的朋友可以參考下2016-07-07php加密之discuz內(nèi)容經(jīng)典加密方式實(shí)例詳解
這篇文章主要介紹了php加密之discuz內(nèi)容經(jīng)典加密方式,結(jié)合具體實(shí)例形式詳細(xì)分析了discuz加密的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02php實(shí)現(xiàn)的http請(qǐng)求封裝示例
這篇文章主要介紹了php實(shí)現(xiàn)的http請(qǐng)求封裝,結(jié)合實(shí)例形式分析了php基于curl的http請(qǐng)求操作功能實(shí)現(xiàn)技巧與使用方法,需要的朋友可以參考下2016-11-11PHP iconv()函數(shù)字符編碼轉(zhuǎn)換的問(wèn)題講解
今天小編就為大家分享一篇關(guān)于PHP iconv()函數(shù)字符編碼轉(zhuǎn)換的問(wèn)題講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03PHP實(shí)現(xiàn)數(shù)組根據(jù)某個(gè)字段進(jìn)行水平合并,橫向合并案例分析
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組根據(jù)某個(gè)字段進(jìn)行水平合并,橫向合并,結(jié)合具體案例形式分析了php數(shù)組遍歷、合并等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10PHP自定session保存路徑及刪除、注銷與寫(xiě)入的方法
這篇文章主要介紹了PHP自定session保存路徑及刪除、注銷與寫(xiě)入的方法,以實(shí)例形式詳細(xì)講述了session的常用操作技巧,在進(jìn)行PHP項(xiàng)目開(kāi)發(fā)的時(shí)候有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11php使用變量動(dòng)態(tài)創(chuàng)建類的對(duì)象用法示例
這篇文章主要介紹了php使用變量動(dòng)態(tài)創(chuàng)建類的對(duì)象,涉及php面向?qū)ο蟪绦蛟O(shè)計(jì)中對(duì)象的動(dòng)態(tài)創(chuàng)建相關(guān)操作技巧,需要的朋友可以參考下2017-02-02