手机验证码

本人所用短信验证码为容联云通讯

发送手机验证码

   function send_sms_code()
{
     Vendor('Sms.REST');
     $mobile = I('phone');
     $code=createSMSCode();
     $one=M('smscode')->where('mobile='.$mobile)->find();
     if($one){
        $data['update_at'] = date('Y-m-d H:i:s');
        $data['code'] = $code;
        M('smscode')->where('mobile='.$mobile)->save($data);
     }else{
         $add = array(
                        "mobile"=>$mobile,

                        "code"=>$code,

                        "create_at"=>date('Y-m-d H:i:s'),
                    );

        M('smscode')->add($add);
     }  
    $serverIP='app.cloopen.com';
    //请求端口
    $serverPort='8883';
    //REST版本号
    $softVersion='2013-12-26';
    //主帐号
    $accountSid=C('RONGLIAN_ACCOUNT_SID');
    //主帐号Token
    $accountToken=C('RONGLIAN_ACCOUNT_TOKEN');
    //应用Id
    $appId=C('RONGLIAN_APPID');
    $rest = new REST($serverIP,$serverPort,$softVersion);
    $rest->setAccount($accountSid,$accountToken);
    $rest->setAppId($appId);    
     // 发送模板短信
   //  echo "Sending TemplateSMS to $to 
"; $result = $rest->sendTemplateSMS($mobile,array("$code",'5'),"403449"); if($result == NULL ) { // echo "result error!"; // break; $this->ajaxReturn(null,"发送错误",0); } if($result->statusCode!=0) { // echo "error code :" . $result->statusCode . "
"; // echo "error msg :" . $result->statusMsg . "
"; $this->ajaxReturn(null,"$result->statusMsg ",0); //TODO 添加错误处理逻辑 }else{ // echo "Sendind TemplateSMS success!
"; // // 获取返回信息 // $smsmessage = $result->TemplateSMS; // echo "dateCreated:".$smsmessage->dateCreated."
"; // echo "smsMessageSid:".$smsmessage->smsMessageSid."
"; //TODO 添加成功处理逻辑 $this->ajaxReturn(null,'发送成功',1); } }

验证短信验证码是否有效

public function checkSMSCode($mobile,$code){

$nowTimeStr = date('Y-m-d H:i:s');
//创建了一个验证码表
$smscode = M('smscode');
$smscodeObj = $smscode->where("mobile='$mobile'")->find();
if($smscodeObj){
 if($smscodeObj['update_at']){
    $smsCodeTimeStr = $smscodeObj['update_at'];
 } else{
    $smsCodeTimeStr = $smscodeObj['create_at'];
 }

 $recordCode = $smscodeObj['code'];
$flag = $this->checkTime($nowTimeStr, $smsCodeTimeStr);  

if(!$flag){

     $this->ajaxReturn(null,'验证码过期,请刷新后重新获取',0);

}
if($code != $recordCode){

     $this->ajaxReturn(null,'验证码错误,请重新输入',0);
}

}else{
     $this->ajaxReturn(null,'未获取验证码',0);
    }
}



验证验证码时间是否过期

public function checkTime($nowTimeStr,$smsCodeTimeStr){ 
$nowTime = strtotime($nowTimeStr);
$smsCodeTime = strtotime($smsCodeTimeStr);
$period = floor(($nowTime-$smsCodeTime)/60); //60s
//5分钟内有效
if($period>=0 && $period<=5){
return true;
}else{
return false;
}
}

你可能感兴趣的:(手机验证码)