關于thinkphp下阿里大于短信驗證

下面由thinkphp框架教程欄目給大家介紹thinkphp下阿里大于短信驗證,希望對需要的朋友有所幫助!

本方法基于Thinkphp,用到了jquery.validate,當然具體項目中也可以不用,下面我就按步驟來寫下來。

1.準備

1.登陸
http://www.alidayu.com,進入網址
用淘寶號登陸即可
2.進入管理中心

關于thinkphp下阿里大于短信驗證

3.配置
(1)配置短信簽名

立即學習PHP免費學習筆記(深入)”;

關于thinkphp下阿里大于短信驗證

關于thinkphp下阿里大于短信驗證

(2)配置短信模板

關于thinkphp下阿里大于短信驗證

關于thinkphp下阿里大于短信驗證

建好后,需要的東西就是APPkey APPsecret

關于thinkphp下阿里大于短信驗證

關于thinkphp下阿里大于短信驗證

還可以進行測試

關于thinkphp下阿里大于短信驗證

2.數據庫

關于thinkphp下阿里大于短信驗證

3.后端

  • 引入
    下載阿里大于類文件,放在/Thinkphp/Library/org/Alidayu(文件夾要大寫,坑過自己,部署到服務器后出問題)

關于thinkphp下阿里大于短信驗證

  • 通用類里

  // 生成短信驗證碼     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;             }         }     }
  • 用的類里,我是放在注冊類里調用

//發送短信,調用BaseController里的方法,因為其他模塊也要用到這方法

  public function send_message(){      $phone=I("post.phone");      //data返回失敗 但不影響使用      $data=$this->send_phone($phone);      $this->ajaxReturn($data,"JSON");  }

4.前臺 ajax部分功能已經具備,還待完善

//發送手機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");         }  }

? 版權聲明
THE END
喜歡就支持一下吧
點贊15 分享