Home >PHP Framework >ThinkPHP >About Alibaba SMS verification under thinkphp
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. Log in to
http://www.alidayu.com, enter the website
and log in with your Taobao account
2. Enter the management center
3. Configuration
(1) Configure SMS signature
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)
// 生成短信验证码 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; } } }
public function send_message(){ $phone=I("post.phone"); //data返回失败 但不影响使用 $data=$this->send_phone($phone); $this->ajaxReturn($data,"JSON"); }
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"); } }
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!