php发送阿里大鱼短信验证码

  • baagee 发布于 2017-04-08 20:03:19
  • 分类:PHP
  • 47884 人围观
  • 28 人喜欢

上个月做的微信站短信验证手机号的功能,即使退出页面重新打开后依然保存上次信息(比如手机号,下次发送验证码倒计时)。

如图所示:


首先从阿里大鱼下载SDK,然后将SDK放入项目里。

发送验证码函数代码:

/*发送验证码*/
public function doMobileSendcode(){
    global $_W,$_GPC;
    /*记录信息*/
    session_start();
    $_SESSION['tellphone']=$_GPC['phone'];
    $_SESSION['uname']=$_GPC['uname'];
    if(isset($_SESSION['time'])){
    	/*未过期*/
    	if($_SESSION['time']+60>time()){
    		echo '一分钟内多次操作';
    	}else{
        	$_SESSION['time']=time();
    	}
    }else{
    	// 记录本次时间
   	    $_SESSION['time']=time();
    }
    include 'PhoneCodeSDK/TopSdk.php';
    date_default_timezone_set('Asia/Shanghai');
    $phonecode=$this->getRandChar(4);
    $c = new TopClient;
    $c->appkey = "**************";
    $c->secretKey = "******************";
    $req = new AlibabaAliqinFcSmsNumSendRequest;
    $req->setSmsType("normal");
    $req->setSmsFreeSignName("身份验证");
    $req->setSmsParam("{product:'潍坊第七医院微信注册',code:'".$phonecode."'}");
    $req->setRecNum($_GPC['phone']);
    $req->setSmsTemplateCode("SMS_40095059");
    $resp = $c->execute($req);
    session_start();
    $_SESSION['phonecode']=$phonecode;
    if($resp->result->success){
        echo '发送成功';
    }else{
        echo '发送失败';
    }
}

//随机字符串
private function getRandChar($length){
    $str = '';
    $strPol = "0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol)-1;
    for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)];
    }
    return $str;
}

前台HTML,js代码:

{template 'common/header'}
<div class="panel panel-info">
  <div class="panel-heading">绑定您的信息</div>
  <div class="panel-body">
  {if isset($error)}
    <div class="alert alert-danger" role="alert">{$error}</div>
  {/if}
    <form action="" method="post">
  <div class="form-group">
    <label for="name">真实姓名:</label>
    <input type="text" class="form-control"  name="name" id="uname" placeholder="名字" {if $ifnum == 0} value="{php echo $_SESSION['uname']}" {/if}  {if $ifnum == 1}disabled="true" value="{$list['name']}"{/if} > 
  </div>
  <div class="form-group">
    <label for="phone">请填写您在医院表单所填写的手机号:</label>
    <input type="text" class="form-control" name="phone" placeholder="电话号" id="tellphone" value="{php echo $_SESSION['tellphone']}" {if $ifnum == 1}disabled="true" value="{$list['phone']}"{/if}>
  </div>
  {if $ifnum == 0}
  <div class="form-group">
  <input type="button" class="btn btn-warning btn-block" value="获取手机验证码"  id="phonebtn" onclick="sendcode(60)"> 
  </div>
  <div class="form-group has-feedback">
    <label class="control-label sr-only"></label>
    <input name="phonecode" type="text" class="form-control" placeholder="验证码">
  </div>
  {/if}
  <input type="hidden" name="token" value="{$_W['token']}" />
  <div style="width: 100%;text-align: center;">
    <button type="submit" name="submit" value="提交" class="btn btn-info">提交</button>
  </div>
  <div class="panel-heading">如有疑问,请联系 0536-00000000000000</div>
</form>
  </div>
</div>


<script>
  {if time()-$_SESSION['time']<60}
    $("#phonebtn").attr('disabled', 'true');
    for(i=1;i<={php echo 60-(time()-$_SESSION['time'])};i++) { 
          window.setTimeout("update_p(" + i + ","+{php echo 60-(time()-$_SESSION['time'])}+")", i * 1000); 
      } 
  {/if}
  function sendcode(t){
    /*发送短信验证码*/
    $("#phonebtn").attr('disabled', 'true');
      for(i=1;i<=t;i++) { 
          window.setTimeout("update_p(" + i + ","+t+")", i * 1000); 
      }
  }

  function update_p(num,t) { 
      if(num == t) { 
          $("#phonebtn").val(" 重新发送 "); 
          $("#phonebtn").removeAttr('disabled');
      }else { 
          printnr = t-num; 
          $("#phonebtn").val(" (" + printnr +")秒后重新发送"); 
      } 
  }
</script>
{template 'footer'}


评论

点击图片切换
还没有评论,快来抢沙发吧!