您好,登录后才能下订单哦!
这篇文章给大家分享的是有关php实现邮箱验证的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
php实现邮箱验证的方法:首先下载phpMailer;然后解压到项目根目录下;接着需要一台邮件服务器;最后在“config.php”中配置邮箱相关的信息并创建用于发送邮件的公用方法即可。
一、Introduction
需求是用户在注册时填写email,注册后,需要到邮箱验证后才能登陆。
二、实现
验证流程,注册成功后,为用户生成一个验证码;将验证码以连接方式发到用户邮箱;用户点击连接将验证码发回网站;网站验证这个验证码是否是为这个用户生成的验证码;帐号需要在24小时内验证,否则帐号失效。
1. 准备工作
需要添加phpMailer类来发邮件。下载phpMailer,然后解压到项目根目录下。
发邮件使用smtp协议,端口是25;收邮件,使用pop3协议,端口号是110。还需要一台邮件服务器。可以自己搭建,也可以使用第三方的。使用第三方的邮件服务器需要在第三方注册一个帐号。登陆开启smtp协议。
在config.php中配置邮箱相关的信息。
   'EMAIL' => array(
       'host' => 'smtp.163.com',
       'user' => 'youxiangzhanghao',
       'password' => 'password', //此处的密码为user邮箱在开启smtp时设置的授权密码。     
),2. 创建用于发送邮件的公用方法
/**
 * 发送邮件的方法
 */
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());
}在控制器的首页方法中测试
//测试发送邮件
var_dump(sendMail('haha@163.com','phptestemail', '<a href="http://www.phptest.com">hahahah</a>'));3. 修改会员表,添加email字段和验证码字段
email varchar(150) not null comment '邮箱', email_chkcode char(32) not null default '' comment '邮箱验证码', email_chkcode_time int unsigned not null comment '邮箱验证码生成时间', key email_chkcode(email_chkcode)
4.修改注册,必须填email
修改注册表单,添加email字段;修改member模型,允许插入email字段,并增加email字段为email的验证规则。
5.注册之前生成email验证码
在_before_insert()方法中
                   //生成email验证码
                   $data['email_chkcode']= md5(uniqid());
                   $data['email_chkcode_time']= time();6. 注册之后将验证码发给用户邮箱
         /**
          * 注册后的钩子函数
          */
         protected function _after_insert($data,$option)
         {
                   $content = "欢迎注册,请点击以下完成验证:<p><a href='http://www.php2.com/eshop/index.php/Home/Member/email_chk/code/{$data['email_chkcode']}'>请点击</a></p>";
                   sendMail($data['email'],'php2网站邮箱验证', $content);
         }7. 在member控制器添加方法完成验证
    /**
     * 完成邮箱验证方法
     */
    public function email_chk()
    {
        //接收验证码
        $code = I('get.code');
        //查询这个验证码是否有效
        $member = D('Admin/Member');
        $info = $member->field('id,email_chkcode_time')->where(array(
            'email_chkcode' => $code,
        ))->find();
        if($info)
        {
            if((time() -$info['email_chkcode_time']) > 86400)
            {
                //帐号过期,删除这个帐号
               $member->delete($info['id']);
                $this->error('验证码已过期,帐号已经删除,请重新注册', U('regist'));
                exit;
            }
            else
            {
                //验证通过,将验证码清空
                $member->where(array(
                    'id' => $info['id'],
               ))->setFielf('email_chkcode', '');
                $this->success('验证通过,请登陆', U('login'));
                exit;
            }
        }
        else
        {
            $this->error('参数错误', U('/'));
            exit;
        }
    }8. 修改会员模型中的登陆方法
         //判断这个帐号是否通过验证码验证
         if(!empty($user['email_chkcode']))
         {
                   $this->error= '必须验证后才可以登陆';
                   returnfalse;
         }9. 修改注册代码
如果一个帐号已经存在,但是过期没有验证,可以删除这个帐号。
添加注册时的表单验证,为username和email添加关于唯一性和注册验证码的验证。
array('email','chk_un_email', 'email已经存在!', 1,'callback', 3),
array('username','chk_un_user', '用户名已经存在!', 1,'callback', 3),
         //验证已经存在的邮箱是否通过验证
         public function chk_un_email()
         {
                   $email = I('post.email');
                   $username =I('post.username');
                   $hasEmail =$this->field('id,email_chkcode,email_chkcode_time')->where(array(
                            'email' =>$email,
                   ))->find();
                   if($hasEmail)
                   {
                            if(empty($hasEmail['email_chkcode']))
                            {
                                     returnfalse;
                            }
                            else
                            {
                                     if(time() -$hasEmail['email_chkcode_time'] > 86400)
                                     {
                                               $this->delete($hasEmail['id']);
                                               returntrue;
                                     }
                                     else
                                     {
                                               returnfalse;
                                     }
                            }
                   }
                   else
                   {
                            return true;
                   }
         }
         //验证已经存在的用户名是否通过验证
         public function chk_un_user()
         {
                   $username =I('post.username');
                   $hasUsername =$this->field('id,email_chkcode,email_chkcode_time')->where(array(
                            'username' =>$username,
                   ))->find();
                   if($hasUsername)
                   {
                            if(empty($hasUsername['email_chkcode']))
                            {
                                     returnfalse;
                            }
                            else
                            {
                                     if(time() -$hasUsername['email_chkcode_time'] > 86400)
                                     {
                                               $this->delete($hasUsername['id']);
                                               returntrue;
                                     }
                                     else
                                     {
                                               returnfalse;
                                     }
                            }
                   }
                   else
                   {
                            return true;
                   }
         }感谢各位的阅读!关于php实现邮箱验证的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。