PHP郵箱驗證樣本教程,php郵箱樣本
在使用者註冊中最常見的安全驗證之一就是郵箱驗證。根據行業的一般做法,進行郵箱驗證是避免潛在的安全隱患一種非常重要的做法,現在就讓我們來討論一下這些最佳實務,來看看如何在PHP中建立一個郵箱驗證。
讓我們先從一個註冊表單開始:
接下來是資料庫的表結構:
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 pageif($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 databaseif(!$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 messagereturn 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');});
郵箱驗證的內容:
Please confirm your e-mail address by clicking the following link: &user=<?php echo $id; ?>">
現在讓我們來驗證一下它是否可行:
$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 messagereturn Redirect::to('verify')->with('error', 'Verification code not valid.');
結論:
上面展示的代碼只是一個教程樣本,並且沒有通過足夠的測試。在你的web應用中使用的時候請先測試一下。上面的代碼是在Laravel架構中完成的,但是你可以很輕鬆的把它遷移到其他的PHP架構中。同時,驗證鏈結接的有效時間為48小時,之後就到期。引入一個工作隊列就可以很好的及時處理那些已經到期的驗證鏈結接。
本文實PHPChina原創翻譯,原文轉載於http://www.phpchina.com/portal.php?mod=view&aid=39888,小編認為這篇文章很具有學習的價值,分享給大家,希望對大家的學習有所協助。
http://www.bkjia.com/PHPjc/1133031.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133031.htmlTechArticlePHP郵箱驗證樣本教程,php郵箱樣本 在使用者註冊中最常見的安全驗證之一就是郵箱驗證。根據行業的一般做法,進行郵箱驗證是避免潛在的安...