国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP如何發送郵件來進行用戶驗證

2020-03-22 18:55:50
字體:
來源:轉載
供稿:網友
網站有時會需要用到郵箱驗證來防止用戶惡意注冊、驗證身份等操作。可是如何使用PHP后端來發送驗證郵件呢?本文就以一套注冊實例來講解PHP是如何去發送郵件的。

在用戶注冊中*常見的安全驗證之一就是郵箱驗證。根據行業的一般做法,進行郵箱驗證是避免潛在的安全隱患一種非常重要的做法,現在就讓我們來討論一下這些*佳實踐,來看看如何在PHP中創建一個郵箱驗證。

讓我們先從一個注冊表單開始:

<form method="post" action="http://mydomain.com/registration/">    <fieldset>        <label for="fname">First Name:</label>        <input type="text" name="fname" required />    </fieldset>    <fieldset>        <label for="lname">Last Name:</label>        <input type="text" name="lname" required />    </fieldset>    <fieldset>        <label for="email">Last name:</label>        <input type="email" name="email" required />    </fieldset>    <fieldset>        <label for="password">Password:</label>        <input type="password" name="password" required />    </fieldset>    <fieldset>        <label for="cpassword">Confirm Password:</label>        <input type="password" name="cpassword" required />    </fieldset>    <fieldset>        <button type="submit">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 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');});


郵箱驗證的內容:

<!DOCTYPE html><html>    <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 messagereturn Redirect::to('verify')->with('error', 'Verification code not valid.');


結論:

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

相關推薦:

php完整驗證碼代碼 php 生成驗證碼 php 短信驗證碼 php驗證碼代

php 短信接口的示例代碼(入門)

php 短信網關短信內容不能有空格,短信網關短信內容_PHP教程

以上就是PHP如何發送郵件來進行用戶驗證的詳細內容,更多請關注 其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绿春县| 安乡县| 上饶市| 华蓥市| 湘潭市| 原阳县| 慈利县| 广元市| 布拖县| 姚安县| 平度市| 红安县| 长春市| 岳西县| 读书| 邹城市| 富裕县| 涪陵区| 榆林市| 张北县| 庆安县| 河南省| 北宁市| 大宁县| 咸丰县| 聂拉木县| 抚州市| 花莲市| 介休市| 榆中县| 黄山市| 内乡县| 山阳县| 安福县| 榆社县| 临海市| 南召县| 汉源县| 洛隆县| 贵州省| 临清市|