javascript - 驗證簡訊驗證碼是否正確

來源:互聯網
上載者:User
關鍵字 javascript php
為了實現簡訊驗證的效果,在jq中,我請求了一次介面,代碼如下:
checked:1,
send:function(){

  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('手機格式錯誤');      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){

// alert(data);

       });    $('.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']一直不會相同,請問大神 這怎麼解決

回複內容:

為了實現簡訊驗證的效果,在jq中,我請求了一次介面,代碼如下:
checked:1,
send:function(){

  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('手機格式錯誤');      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){

// alert(data);

       });    $('.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']一直不會相同,請問大神 這怎麼解決

用session等方式儲存最後一次產生的驗證碼用來和使用者輸入作比對...

這個問題其實跟你的curl請求有關,因為你這樣請求是沒有攜帶session的,因此每次都相當於是一個新的使用者在一個獨立的瀏覽器上去請求,那麼每次的結果必然不一樣了;
因此你要加上一個:

curl_setopt($ch, CURLOPT_COOKIEJAR, $file);    //這個具體的你去看看手冊就知道了

已經解決這個問題,我是用ajax傳電話號碼到後台,然後從後台請求介面,順帶擷取介面傳回值,然後用介面傳回值和使用者提交過來的驗證碼比較

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.