小編給大家分享一下PHP郵箱驗證的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、下冶網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為下冶等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。讓我們先從一個注冊表單開始:
<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>
接下來是數(shù)據(jù)庫的表結(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, );
一旦這個表單被提交了,我們就需要驗證用戶的輸入并且創(chuàng)建一個新用戶:
// 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.');
注冊之后,用戶的賬戶仍然是無效的直到用戶的郵箱被驗證。此功能確認(rèn)用戶是輸入電子郵件地址的所有者,并有助于防止垃圾郵件以及未經(jīng)授權(quán)的電子郵件使用和信息泄露。
整個流程是非常簡單的——當(dāng)一個新用戶被創(chuàng)建時,在注冊過過程中,一封包含驗證鏈接的郵件便會被發(fā)送到用戶填寫的郵箱地址中。在用戶點擊郵箱驗證鏈接和確認(rèn)郵箱地址之前,用戶是不能進(jìn)行登錄和使用網(wǎng)站應(yīng)用的。
關(guān)于驗證的鏈接有幾件事情是需要注意的。驗證的鏈接需要包含一個隨機(jī)生成的token,這個token應(yīng)該足夠長并且只在一段時間段內(nèi)是有效的,這樣做的方法是為了防止網(wǎng)絡(luò)攻擊。同時,郵箱驗證中也需要包含用戶的標(biāo)識,這樣就可以避免那些攻擊多用戶的潛在危險。
現(xià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;
一旦這個驗證被創(chuàng)建就把他存儲到數(shù)據(jù)庫中,發(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'); });
郵箱驗證的內(nèi)容:
<!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>
現(xià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.');
以上是“PHP郵箱驗證的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
新聞標(biāo)題:PHP郵箱驗證的示例分析-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://aaarwkj.com/article40/ccccho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、全網(wǎng)營銷推廣、動態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容