(進階篇)PHP(thinkphp5框架)實現用戶註冊後郵箱驗證,激活賬號

本文將結合實例,講解如何使用thinkphp5+Mysql完成註冊賬號、發送激活郵件、驗證激活賬號、處理URL連接過時的功能。php

業務流程

一、用戶提交註冊信息。html

二、寫入數據庫,此時賬號狀態未激活。sql

三、將用戶名密碼或其餘標識字符加密構形成激活識別碼(你也能夠叫激活碼)。thinkphp

四、將構造好的激活識別碼組成URL發送到用戶提交的郵箱。數據庫

五、用戶登陸郵箱並點擊URL,進行激活。apache

六、驗證激活識別碼,若是正確則激活賬號。瀏覽器

準備數據表

用戶信息表中字段Email很重要,它能夠用來驗證用戶、找回密碼、甚至對網站方來講能夠用來收集用戶信息進行Email營銷,如下是用戶信息表cmf_email的表結構:服務器

CREATE TABLE `cmf_email`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `email` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '郵箱',
  `email_password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '郵箱註冊碼',
  `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '帳戶激活碼',
  `token_exptime` int(10) NULL DEFAULT NULL COMMENT '激活碼有效期',
  `status` tinyint(1) NULL DEFAULT 0 COMMENT '狀態,0=未激活,1=已激活',
  `regtime` int(10) NULL DEFAULT NULL COMMENT '註冊時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 25 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '//---前臺郵箱註冊' ROW_FORMAT = Dynamic;

 

 

 

 

在框架中下載phpmailer包app

1.composer require phpmailer/phpmailercomposer

2.在控制器中引入類

use PHPMailer\PHPMailer\PHPMailer

 

 

3步驟 

 

 

<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\facade\Validate;
use app\user\model\UserModel;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class RegisterController extends HomeBaseController
{

    /**
     * 前臺用戶註冊
     */
    public function index()
    {
        return $this->fetch('register');
    }



    //發送qq郵箱
    /*
     * @param
     *  $address_email --收件人郵箱
     *  $active_url ---激活地址
     *  $token --- 帳戶激活碼
     *  $email_password --郵箱密碼
     * **/
    public function setEmail($address_email, $token, $active_url, $email_password)
    {
        $sendmail = '135xxx7@qq.com'; //發件人郵箱
        //$sendmailpswd = "cbllxxxxxxxhdbc"; //客戶端受權密碼,而不是郵箱的登陸密碼!
        $sendmailpswd = "olbzxxxxhpqjbfc"; //客戶端受權密碼,而不是郵箱的登陸密碼!
        $send_name    = '悅桔拉拉商城';// 設置發件人信息,如郵件格式說明中的發件人,
        $toemail      = $address_email;//定義收件人的郵箱
        $to_name      = '小號';//設置收件人信息,如郵件格式說明中的收件人
        $mail         = new PHPMailer();
        $mail->isSMTP();// 使用SMTP服務
        $mail->CharSet    = "utf8";// 編碼格式爲utf8,不設置編碼的話,中文會出現亂碼
        $mail->Host       = "smtp.qq.com";// 發送方的SMTP服務器地址
        $mail->SMTPAuth   = true;// 是否使用身份驗證
        $mail->Username   = $sendmail;//// 發送方的
        $mail->Password   = $sendmailpswd;//客戶端受權密碼,而不是郵箱的登陸密碼!
        $mail->SMTPSecure = "ssl";// 使用ssl協議方式
        $mail->Port       = 465;//  qq端口465或587)
//        $mail->setFrom($sendmail,$send_name);// 設置發件人信息,如郵件格式說明中的發件人,
        $mail->setFrom($sendmail, $send_name);
        $mail->addAddress($toemail, $to_name);// 設置收件人信息,如郵件格式說明中的收件人,
        $mail->addReplyTo($sendmail, $send_name);// 設置回覆人信息,指的是收件人收到郵件後,若是要回復,回覆郵件將發送到的郵箱地址
        //$mail->addCC("xxx@qq.com");// 設置郵件抄送人,能夠只寫地址,上述的設置也能夠只寫地址(這我的也能收到郵件)
        //$mail->addBCC("xxx@qq.com");// 設置祕密抄送人(這我的也能收到郵件)
        //$mail->addAttachment("bug0.jpg");// 添加附件
        $mail->Subject = "悅桔拉拉商城,激活郵箱";// 郵件標題
//        $mail->Body = "郵件內容是 <b>您的驗證碼是:123456</b>,哈哈哈!";// 郵件正文
        $mail->Body = "恭喜您,註冊成功!請點擊連接激活您的賬戶:" . "$active_url" . "$token" . "
    若是以上連接沒法點擊,請將它複製到你的瀏覽器地址欄中進入訪問,該連接24小時內有效。";// 郵件正文
        //$mail->AltBody = "This is the plain text純文本";// 這個是設置純文本方式顯示的正文內容,若是不支持Html方式,就會用到這個,基本無用
        $token_exptime = time() + 60 * 60 * 24;//過時時間爲24小時後
        if (!$mail->send()) {// 發送郵件
            $this->error('郵箱註冊失敗!請檢查郵箱號碼是否正確', url('user/register/index'));
        } else {
            //將郵箱與密碼寫入數據庫
            $data = [
                'email'          => $address_email,
                'email_password' => $email_password,
                'token'          => $token,
                'regtime'        => time(),
                'token_exptime'  => $token_exptime,
            ];
            $res  = Db::name('email')->insert($data);
            if ($res) {
                $this->success('恭喜您,註冊成功!<br/>請登陸到您的郵箱及時激活您的賬號,而後進行登陸!', url('user/login/index'));
            }
        }
    }
  
  //前臺註冊頁面,用戶點擊提交,跳轉到控制器裏面的add方法
public function add() { if ($this->request->isPost()) { $rules = [ 'email' => $this->request->param('email'), 'email_password' => cmf_password($this->request->param('email_password'), $authCode = 'yjllshop'), ]; $nowtime = time(); //當前時間 //對因而否已經註冊用戶進行判斷 $res = Db('email')->where('email', $rules['email'])->find(); if ($res) { if ($nowtime > $res['token_exptime']) { $this->error('您的激活有效期已過,請登陸您的賬號從新發送激活郵件', url('user/register/index')); } else { $this->success('您已經註冊過,請直接登陸!', url('user/login/index')); } } //dump($rules);die; //激活地址--對應激活方法 $active_url = 'http://www.yjllshop.com/user/register/valRegister?token='; //調用生產token方法 $token = $this->makeToken($rules['email']); $this->setEmail($rules['email'], $token, $active_url, $rules['email_password']); } } //製做token public function makeToken($email) { $regtime = time(); $num = rand(0, 100);//一段隨機數字 $md5Num = md5($regtime . $num . $email); $token = base64_encode(md5($md5Num)); //建立用於激活識別碼 return $token; } //郵箱激活方法--而且將郵箱的各個信息存放數據庫 public function valRegister() { //$token $nowtime = time(); //當前時間 if ($this->request->isGet()) { $token = $this->request->param('token'); //將條件token值與status=0狀態值帶入數據庫查詢,若是能查到,在判斷時間是夠是過時,就進行激活操做,改變激活碼 $res = Db('email') ->where('status', 0) ->where('token', $token)->find(); //dump($res); /// dump($res['token_exptime']);die; if ($res) { if ($nowtime > $res['token_exptime']) { $this->error('您的激活有效期已過,請登陸您的賬號從新發送激活郵件', url('user/register/index')); } else { Db::name('email')->where('token', $token)->setField('status', 1); $this->success('恭喜您,激活成功!<br/>請進行登陸!', url('user/login/index')); } } else { $this->error('郵箱註冊失敗!請檢查郵箱號碼是否正確', url('user/register/index')); } } //Db::name('email')->insert($user); } }

  

  

客戶端的受權碼 

註冊完成後的數據庫

相關文章
相關標籤/搜索