Home > Backend Development > PHP Tutorial > javascript - 验证短信验证码是否正确

javascript - 验证短信验证码是否正确

WBOY
Release: 2016-06-06 20:12:42
Original
1491 people have browsed it

为了实现短信验证的效果,在jq中,我请求了一次接口,代码如下:
checked:1,
send:function(){

<code>  var numbers = /^1\d{10}$/;
  var val = $('#mobile').val().replace(/\s+/g,""); //获取输入手机号码
  if($('.yzm').find('span').length == 0 && $('.yzm a').attr('class') == 'send1'){
    if(!numbers.test(val) || val.length ==0){
      $('.yzm').append('<span class="error01">手机格式错误</span>');
      return false;
    }
  }
  if(numbers.test(val)){
    var time = 60;
    $('.yzm span').remove();
    function timeCountDown(){
      if(time==0){
        clearInterval(timer);
        $('.yzm a').addClass('send1').removeClass('send0').html("发送验证码");
        sends.checked = 1;
        return true;
      }

      $('.yzm a').html(time+"S后再次发送");
      
      time--;

      return false;
      sends.checked = 0;
    }
//jq中的方位接口  得到一个验证码  
  var url = "http://192.144.1.168/MJS/user/verificationSign"+"?userAccounts="+$("#mobile").val();
  
  $.get(url,function(data){</code>
Copy after login
Copy after login

// alert(data);

<code>   
    });
    $('.yzm a').addClass('send0').removeClass('send1');
    timeCountDown();
    var timer = setInterval(timeCountDown,1000);
  }
  然后再php页面中,用curl获取接口返回值,又请求了一次,代码如下:
  
  //在php页面中请求接口
  $url = 'http://192.144.1.168/MJS/user/verificationSign?userAccounts='.$userAccounts; //我们统一用的是JSON格式
                $ch = curl_init();  
                $timeout = 5;  
                curl_setopt($ch, CURLOPT_URL, $url);  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
                curl_setopt($ch, CURLOPT_POST, 1);  
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
                $contents = curl_exec($ch);  
                curl_close($ch);  
                $body = json_decode ($contents, TRUE);

                if($mcod != $body['code'])
                {
                    echo "验证码不正确,请输入正确的验证码";
                    
                    这样就又有一个验证码生成,$mcod != $body['code']一直不会相同,请问大神 这怎么解决</code>
Copy after login
Copy after login

回复内容:

为了实现短信验证的效果,在jq中,我请求了一次接口,代码如下:
checked:1,
send:function(){

<code>  var numbers = /^1\d{10}$/;
  var val = $('#mobile').val().replace(/\s+/g,""); //获取输入手机号码
  if($('.yzm').find('span').length == 0 && $('.yzm a').attr('class') == 'send1'){
    if(!numbers.test(val) || val.length ==0){
      $('.yzm').append('<span class="error01">手机格式错误</span>');
      return false;
    }
  }
  if(numbers.test(val)){
    var time = 60;
    $('.yzm span').remove();
    function timeCountDown(){
      if(time==0){
        clearInterval(timer);
        $('.yzm a').addClass('send1').removeClass('send0').html("发送验证码");
        sends.checked = 1;
        return true;
      }

      $('.yzm a').html(time+"S后再次发送");
      
      time--;

      return false;
      sends.checked = 0;
    }
//jq中的方位接口  得到一个验证码  
  var url = "http://192.144.1.168/MJS/user/verificationSign"+"?userAccounts="+$("#mobile").val();
  
  $.get(url,function(data){</code>
Copy after login
Copy after login

// alert(data);

<code>   
    });
    $('.yzm a').addClass('send0').removeClass('send1');
    timeCountDown();
    var timer = setInterval(timeCountDown,1000);
  }
  然后再php页面中,用curl获取接口返回值,又请求了一次,代码如下:
  
  //在php页面中请求接口
  $url = 'http://192.144.1.168/MJS/user/verificationSign?userAccounts='.$userAccounts; //我们统一用的是JSON格式
                $ch = curl_init();  
                $timeout = 5;  
                curl_setopt($ch, CURLOPT_URL, $url);  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
                curl_setopt($ch, CURLOPT_POST, 1);  
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
                $contents = curl_exec($ch);  
                curl_close($ch);  
                $body = json_decode ($contents, TRUE);

                if($mcod != $body['code'])
                {
                    echo "验证码不正确,请输入正确的验证码";
                    
                    这样就又有一个验证码生成,$mcod != $body['code']一直不会相同,请问大神 这怎么解决</code>
Copy after login
Copy after login

用session等方式保存最后一次生成的验证码用来和用户输入作比对...

这个问题其实跟你的curl请求有关,因为你这样请求是没有携带session的,因此每次都相当于是一个新的用户在一个独立的浏览器上去请求,那么每次的结果必然不一样了;
因此你要加上一个:

<code>curl_setopt($ch, CURLOPT_COOKIEJAR, $file);    //这个具体的你去看看手册就知道了</code>
Copy after login

已经解决这个问题,我是用ajax传电话号码到后台,然后从后台请求接口,顺带获取接口返回值,然后用接口返回值和用户提交过来的验证码比较

Related labels:
source:php.cn
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