Home  >  Article  >  Backend Development  >  PHP implements email verification

PHP implements email verification

藏色散人
藏色散人Original
2020-09-30 10:06:406404browse

php method to implement email verification: first download phpMailer; then unzip it to the project root directory; then you need a mail server; finally configure the email-related information in "config.php" and create a file for sending The public method of mail will do.

PHP implements email verification

Recommended: "PHP Video Tutorial"

1. Introduction

Requirements The user fills in their email when registering. After registration, they need to verify their email before they can log in.

2. Implement the

verification process. After successful registration, a verification code is generated for the user; the verification code is sent to the user's mailbox in the form of a connection; the user clicks the connection to send the verification code back to the website; The website verifies whether the verification code is generated for this user; the account needs to be verified within 24 hours, otherwise the account will become invalid.

1. Preparation

You need to add the phpMailer class to send emails. Download phpMailer and extract it to the project root directory.

Send emails using the smtp protocol, the port is 25; receive emails, use the pop3 protocol, the port number is 110. A mail server is also required. You can build it yourself or use a third-party one. Using a third-party mail server requires registering an account with the third party. Log in and enable the smtp protocol.

Configure email-related information in config.php.

   'EMAIL' => array(

       'host' => 'smtp.163.com',

       'user' => 'youxiangzhanghao',

       'password' => 'password', //此处的密码为user邮箱在开启smtp时设置的授权密码。     

),

2. Create a public method for sending emails

/**

 * 发送邮件的方法

 */

functionsendMail($to, $title, $content)

{

         require_once('./PHPMailer-5.2.14/class.smtp.php');

         require_once('./PHPMailer-5.2.14/class.phpmailer.php');

         $mail = new PHPMailer();

         //设置为发送邮件

         $mail->IsSMTP();

         //是否允许发送html代码为邮件内容

         $mail->IsHTML(true);

         $mail->CharSet = 'utf-8';

         //是否需要身份验证

         $mail->SMTPAuth = true;

         //邮件服务器的帐号信息

         $mailConfig = C('EMAIL');

         $mail->From =$mailConfig['user'].'@163.com';

         $mail->FromName =$mailConfig['user'];

         $mail->Host = $mailConfig['host'];

         $mail->Username = $mailConfig['user'];

         $mail->Password =$mailConfig['password'];

         //发邮件的端口号

         $mail->Port = 25;

         //收件人

         $mail->AddAddress($to);

         //邮件标题

         $mail->Subject = $title;

         //邮件内容

         $mail->Body = $content;

         return($mail->send());

}

Test in the controller's homepage method

//测试发送邮件

var_dump(sendMail(&#39;haha@163.com&#39;,&#39;phptestemail&#39;, &#39;<a href="http://www.phptest.com">hahahah</a>&#39;));

3. Modify the member table, add email field and verification Code field

email  varchar(150)  not  null comment  &#39;邮箱&#39;,

email_chkcode  char(32)  not  null default  &#39;&#39;  comment  &#39;邮箱验证码&#39;,

email_chkcode_time  int  unsigned  not  null comment  &#39;邮箱验证码生成时间&#39;,

key  email_chkcode(email_chkcode)

4. Modify registration, email must be filled in

Modify the registration form, add the email field; modify the member model to allow the insertion of the email field, and add the verification rule for the email field as email.

5. Generate email verification code before registration

In the _before_insert() method

                   //生成email验证码

                   $data[&#39;email_chkcode&#39;]= md5(uniqid());

                   $data[&#39;email_chkcode_time&#39;]= time();

6. After registration, send the verification code to the user’s email address

         /**

          * 注册后的钩子函数

          */

         protected function _after_insert($data,$option)

         {

                   $content = "欢迎注册,请点击以下完成验证:<p><a href=&#39;http://www.php2.com/eshop/index.php/Home/Member/email_chk/code/{$data[&#39;email_chkcode&#39;]}&#39;>请点击</a></p>";

                   sendMail($data[&#39;email&#39;],&#39;php2网站邮箱验证&#39;, $content);

         }

7.                             Add a method in the member controller to complete the verification

    /**

     * 完成邮箱验证方法

     */

    public function email_chk()

    {

        //接收验证码

        $code = I(&#39;get.code&#39;);

        //查询这个验证码是否有效

        $member = D(&#39;Admin/Member&#39;);

        $info = $member->field(&#39;id,email_chkcode_time&#39;)->where(array(

            &#39;email_chkcode&#39; => $code,

        ))->find();

        if($info)

        {

            if((time() -$info[&#39;email_chkcode_time&#39;]) > 86400)

            {

                //帐号过期,删除这个帐号

               $member->delete($info[&#39;id&#39;]);

                $this->error(&#39;验证码已过期,帐号已经删除,请重新注册&#39;, U(&#39;regist&#39;));

                exit;

            }

            else

            {

                //验证通过,将验证码清空

                $member->where(array(

                    &#39;id&#39; => $info[&#39;id&#39;],

               ))->setFielf(&#39;email_chkcode&#39;, &#39;&#39;);

                $this->success(&#39;验证通过,请登陆&#39;, U(&#39;login&#39;));

                exit;

            }

        }

        else

        {

            $this->error(&#39;参数错误&#39;, U(&#39;/&#39;));

            exit;

        }

    }

8.                                                                                                                 adding a method to complete the verification

         //判断这个帐号是否通过验证码验证

         if(!empty($user[&#39;email_chkcode&#39;]))

         {

                   $this->error= &#39;必须验证后才可以登陆&#39;;

                   returnfalse;

         }

8.                                           out out out out out of ’s to be verified to be verified to have It exists, but it has expired and has not been verified. You can delete this account.

Add form verification during registration, and add verification on uniqueness and registration verification code for username and email.

           array('email','chk_un_email', 'email already exists!', 1,'callback', 3),

                                      The name already exists!', 1,'callback', 3),

         //验证已经存在的邮箱是否通过验证

         public function chk_un_email()

         {

                   $email = I(&#39;post.email&#39;);

                   $username =I(&#39;post.username&#39;);

                   $hasEmail =$this->field(&#39;id,email_chkcode,email_chkcode_time&#39;)->where(array(

                            &#39;email&#39; =>$email,

                   ))->find();

                   if($hasEmail)

                   {

                            if(empty($hasEmail[&#39;email_chkcode&#39;]))

                            {

                                     returnfalse;

                            }

                            else

                            {

                                     if(time() -$hasEmail[&#39;email_chkcode_time&#39;] > 86400)

                                     {

                                               $this->delete($hasEmail[&#39;id&#39;]);

                                               returntrue;

                                     }

                                     else

                                     {

                                               returnfalse;

                                     }

                            }

                   }

                   else

                   {

                            return true;

                   }

         }



         //验证已经存在的用户名是否通过验证

         public function chk_un_user()

         {

                   $username =I(&#39;post.username&#39;);

                   $hasUsername =$this->field(&#39;id,email_chkcode,email_chkcode_time&#39;)->where(array(

                            &#39;username&#39; =>$username,

                   ))->find();

                   if($hasUsername)

                   {

                            if(empty($hasUsername[&#39;email_chkcode&#39;]))

                            {

                                     returnfalse;

                            }

                            else

                            {

                                     if(time() -$hasUsername[&#39;email_chkcode_time&#39;] > 86400)

                                     {

                                               $this->delete($hasUsername[&#39;id&#39;]);

                                               returntrue;

                                     }

                                     else

                                     {

                                               returnfalse;

                                     }

                            }

                   }

                   else

                   {

                            return true;

                   }

         }

10. Test

The above is the detailed content of PHP implements email verification. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn