Home > PHP Framework > ThinkPHP > body text

About Alibaba SMS verification under thinkphp

藏色散人
Release: 2020-12-18 09:07:07
forward
2497 people have browsed it

The following tutorial column of thinkphp framework will introduce to you the Alibaba SMS verification under thinkphp. I hope it will be helpful to friends in need!

This method is based on Thinkphp and uses jquery.validate. Of course, it does not need to be used in specific projects. I will write down the steps below.

1. Preparation

1. Log in to
http://www.alidayu.com, enter the website
and log in with your Taobao account
2. Enter the management center

About Alibaba SMS verification under thinkphp

3. Configuration
(1) Configure SMS signature

About Alibaba SMS verification under thinkphp

About Alibaba SMS verification under thinkphp

##(2) Configure SMS template

About Alibaba SMS verification under thinkphp

About Alibaba SMS verification under thinkphp

After it is built, the only thing needed is APPkey APPsecret

About Alibaba SMS verification under thinkphp

About Alibaba SMS verification under thinkphp

You can also test

About Alibaba SMS verification under thinkphp

2. Database

About Alibaba SMS verification under thinkphp

3. Backend

  • Introduction

    Download the Alidayu class file and place it in /Thinkphp/Library/org/Alidayu (the folder must be capitalized to avoid cheating yourself) , a problem occurred after deploying to the server)

About Alibaba SMS verification under thinkphp

    ##In the general class
  •   // 生成短信验证码
        public function createSMSCode($length = 4){
            $min = pow(10 , ($length - 1));
            $max = pow(10, $length) - 1;
            return rand($min, $max);
        }
    
        //发送验证码
        public function send_phone($phone){
            $code=$this->createSMSCode($length = 4);
            import('Org.Alidayu.top.TopClient');
            import('Org.Alidayu.top.ResultSet');
            import('Org.Alidayu.top.RequestCheckUtil');
            import('Org.Alidayu.top.TopLogger');
            import('Org.Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
            $c = new \TopClient;
            $appkey="你的appkey";
            $secret="你的secret;
            $c ->appkey = $appkey ;
            $c ->secretKey = $secret ;
            $req = new \AlibabaAliqinFcSmsNumSendRequest;
            $req ->setExtend( "" );
            $req ->setSmsType( "normal" );
            $req ->setSmsFreeSignName( "阿尚测试" );
            $req ->setSmsParam( "{name:'客户',code:'".$code."',time:'5分钟'}" );
            $req ->setRecNum( $phone);
            $req ->setSmsTemplateCode( "SMS_71300157" );
            $resp = $c ->execute( $req );
            $this->sendMsgResult($resp,$phone,$code);
        }
        
        //验证手机号是否发送成功  前端用ajax,发送成功则提示倒计时,如50秒后可以重新发送
        private function sendMsgResult($resp,$phone,$code){
            if ($resp->result->success && !$resp->result->code) {
                $data['phone']=$phone;
                $data['code']=$code;
                $data['send_time']=time();
                $result=M("code")->add($data);
                if($result){
                    $data="发送成功";
                }else{
                    $data="发送失败";
                }
            } else if ($resp->code || $resp->msg == "Remote service error") {
                $data="发送失败";
            } else {
                $data="发送失败";
            }
            return $data;
        }
        
        // 验证短信验证码是否有效,前端用jquery validate的remote
        public function checkSMSCode(){
            $mobile = $_POST['mobile'];
            $code = $_POST['code'];
            $nowTimeStr = date('Y-m-d H:i:s');
            $smscodeObj = M("code")->where("mobile='$mobile'")->find();
            if($smscodeObj){
                $smsCodeTimeStr = $smscodeObj['send_time'];
                $recordCode = $smscodeObj['code'];
                $flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);
                if($flag&&$code==$recordCode){
                    echo true;
                }else{
                    echo false;
                }
            }
        }
    Copy after login
    In the class used, I put it in the registration class to call
  • //Send text messages and call the method in BaseController, because other modules also need to use it This method
  public function send_message(){
     $phone=I("post.phone");
     //data返回失败 但不影响使用
     $data=$this->send_phone($phone);
     $this->ajaxReturn($data,"JSON");
 }
Copy after login

4. Part of the front-end ajax function is already available and needs to be improved

//发送手机ajax
function send_message() {
        if($("#phone input").valid()){
            var phone=$("#phone input").val();
            $.post("{:U('Register/send_message')}","phone="+phone,function(data){
                
            });    
            $("#send_message").html("发送成功");
            $("#send_message").css("background-color","#2f9cff");
        }

}
Copy after login

The above is the detailed content of About Alibaba SMS verification under thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!