首頁  >  文章  >  後端開發  >  PHP郵件信箱驗證實例詳解

PHP郵件信箱驗證實例詳解

墨辰丷
墨辰丷原創
2018-06-02 09:20:291754瀏覽

這篇文章主要為大家詳細介紹了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 &#39;0&#39;,
 `verify_token` VARCHAR(255) ,
 `created_at` TIMESTAMP,
 `updated_at` TIMESTAMP,
);

一旦這個表單被提交了,我們就需要驗證使用者的輸入並且建立新使用者:

// Validation rules
$rules = array(
  &#39;fname&#39; => &#39;required|max:255&#39;,
  &#39;lname&#39; => &#39;required|max:255&#39;,
 &#39;email&#39; => &#39;required&#39;,
 &#39;password&#39; => &#39;required|min:6|max:20&#39;,
 &#39;cpassword&#39; => &#39;same:password&#39;
);

$validator = Validator::make(Input::all(), $rules);

// If input not valid, go back to registration page
if($validator->fails()) {
 return Redirect::to(&#39;registration&#39;)->with(&#39;error&#39;, $validator->messages()->first())->withInput();
}

$user = new User();
$user->fname = Input::get(&#39;fname&#39;);
$user->lname = Input::get(&#39;lname&#39;);
$user->password = Input::get(&#39;password&#39;);

// 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(&#39;registration&#39;)->with(&#39;error&#39;, &#39;Unable to write to database at this time. Please try again later.&#39;)->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(&#39;registration&#39;)->with(&#39;success&#39;, &#39;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.&#39;);

 註冊之後,使用者的帳戶仍然是無效的直到使用者的郵箱被驗證。此功能可確認使用者是輸入電子郵件地址的擁有者,並有助於防止垃圾郵件以及未經授權的電子郵件使用和資訊外洩。

 整個流程是非常簡單的-當一個新使用者被建立時,在註冊過程中,一封包含驗證連結的郵件就會被傳送到使用者填寫的郵箱地址。在使用者點擊郵箱驗證連結和確認郵箱地址之前,使用者是無法進行登入和使用網站應用的。

 關於驗證的連結有幾件事情是需要注意的。驗證的連結需要包含一個隨機產生的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(&#39;emails.email-confirmation&#39;, array(&#39;code&#39; => $code, &#39;id&#39; => $user->id), function($message)
{
$message->from(&#39;my@domain.com&#39;, &#39;Mydomain.com&#39;)->to($user->email, $user->fname . &#39; &#39; . $user->lname)->subject(&#39;Mydomain.com: E-mail confirmation&#39;);
});

郵箱驗證的內容:

<!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(&#39;id&#39;, &#39;=&#39;, Input::get(&#39;user&#39;))
  ->where(&#39;is_active&#39;, &#39;=&#39;, 0)
  ->where(&#39;verify_token&#39;, &#39;=&#39;, Input::get(&#39;code&#39;))
  ->where(&#39;created_at&#39;, &#39;>=&#39;, 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(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Unable to connect to database at this time. Please try again later.&#39;);
 }

 // Show the success message
 return Redirect::to(&#39;verify&#39;)->with(&#39;success&#39;, &#39;You account is now active. Thank you.&#39;);
}

// Code not valid, show error message
return Redirect::to(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Verification code not valid.&#39;);

結論:
上面展示的程式碼只是一個教學範例,並且沒有通過足夠的測試。在你的web應用程式中使用的時候請先測試一下。上面的程式碼是在Laravel框架中完成的,但你可以輕鬆的把它遷移到其他的PHP框架。同時,驗證連結的有效時間為48小時,之後就過期。引入一個工作隊列就可以很好的及時處理那些已經過期的驗證連結。

以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

基於PHP實作身分證校驗碼運算方法

PHP實作四個基礎排序演算法的運算時間比較(必讀)

基於PHP使用加鎖實作並發情況下搶碼功能的方法

#

以上是PHP郵件信箱驗證實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn