thinkPHP实现发送邮箱验证,实现用户账号激活功能

浏览:7131 发布日期:2016/12/08 分类:功能实现 关键字: 邮箱验证 账号激活 账号状态
thinkPHP实现发送邮箱验证,实现用户账号激活功能
邮件发送功能,请参考http://www.thinkphp.cn/code/989.html

这里是借用了这位的邮件发送类经过修改实现邮件发送
在common/function.php文件里加上邮件发送的方法sendEmailTofunction sendEmailTo($to,$username,$emailtitle,$content){
    $emaildate=date('Y-m-d h:i:s',time());
    $emailcontent.='<html><head></head><body><div style="font-family:黑体;min-height:300px; background:#57bfaa;min-width:300px;max-width: 1000px;border: 0px solid #ccc; margin: auto;">';
    $emailcontent.='<div style="width: 100%;font-size:20px;text-align: center;background: #4484c5; height: 50px;color: #FFF;line-height: 50px">邮件提醒</div>';
    $emailcontent.='<div style="padding: 20px;color: #fff">';
    $emailcontent.='<h3>尊敬的【'.$username.'】你好:</h3>';
    $emailcontent.='<p style="line-height: 30px">'.$content.'</p>';
    $emailcontent.='<p style="line-height: 30px">此邮件为系统自动发送,请勿直接回复!</p>';
    $emailcontent.='<p style="text-align: right;">XXX团队</p>';
    $emailcontent.='<p style="text-align: right;">'.$emaildate.'</p>';
    $emailcontent.='</div>';
    $emailcontent.='</div></body></html>';
    if(SendMail($to,$emailtitle,$emailcontent)){
        return true;
    }else{
        return false;
    }
}
在控制器里面写sendCheckEmail事件。 public function sendCheckEmail(){
        $user=M('user');
        $email=($_SESSION['user_email']);
        if($_SESSION['user_email']==""){
            $this->show('还未登录');
        }
        $usersta=$user->where(array('email'=>$email,'stat'=>0))->select();
        if(!$usersta){
             $this->success('你的账号已经激活,不需要再次激活!','/index/login'); 
        }else{
            if(IS_POST){
                $user=M('user');
                $emailID=passport_encrypt($email);
                $emailkey=md5(randStr(6,3));
                $keydate=time();
                $result=$user->where(array('email'=>$email))->setField(array('email_key'=>$emailkey,'datatime'=>$keydate));
                $content="注册成功,你在本站注册的邮箱需要验证!请点击<a href='http://localhost/index/checkid/?emailkey=".passport_encrypt($emailkey)."&email=".$emailID."'>http://localhost/index/checkid/?emailkey=".passport_encrypt($emailkey)."&email=".$emailID."</a>(或者复制到浏览器打开),完成验证!";
                $ema=sendEmailTo($email, '用户', '邮箱验证', $content);
                if($ema){
                    $data['code']="1101";
                    $data['status']="发送成功";
                }else{
                    $data['code']="1102";
                    $data['status']="邮件发送失败";
                }
                $this->ajaxReturn($data);
            }else{
                $this->assign('email',$email);
                $this->display();
            }
        }
    }
这里面的randStr方法是随机生成6位字符串通过MD5加密生成邮件验证key
passport_encrypt方法是本站应用到的可以加密解密的加密方式将邮箱加密,发送激活链接时将邮箱号加密
passport_decrypt是解密方式
激活操作 public function checkid(){
        $get=$_GET;
        $user=M('user');
        $result=$user->where( array('email'=>passport_decrypt($get['email']),'email_key'=>passport_decrypt($get['emailkey'])))->select();
        if ($result){
            $keytime=$result[0]['datatime'];
            $presenttime=time();
            $agotime=($presenttime-$keytime);
            if($agotime>3600){
                echo "超过10分钟,链接失效";
            }else{
                 $result=$user->where( array('email'=>passport_decrypt($get['email']),'email_key'=>passport_decrypt($get['emailkey'])))->setField('stat','1'); 
                 $this->success('激活成功','/Index/index');
            }
        }
        else{
            $this->success('激活失败重新激活','/Index/sendCheckEmail');
        }   
    }
生成随机字符串函数function randStr($length=4,$type="1"){
    $array = array(
        '1' => '0123456789',
        '2' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        '3' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
    );
    $string = $array[$type];
    $count = strlen($string)-1;
    $rand = '';
    for ($i = 0; $i < $length; $i++) {
        $rand .= $string[mt_rand(0, $count)];
    }
    return $rand;
}
thinkPHP交流群:340036554
评论( 相关
后面还有条评论,点击查看>>