為了實現簡訊驗證的效果,在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傳電話號碼到後台,然後從後台請求介面,順帶擷取介面傳回值,然後用介面傳回值和使用者提交過來的驗證碼比較