php實(shí)現(xiàn)短信驗(yàn)證的方法:首先接入短信服務(wù);然后在網(wǎng)站信息提交頁(yè)面請(qǐng)求發(fā)送信息;接著服務(wù)器向短信服務(wù)提供商通信,提交發(fā)送請(qǐng)求;最后短信服務(wù)提供商通過(guò)運(yùn)營(yíng)商將信息發(fā)送到用戶(hù)的手機(jī)中。
php實(shí)現(xiàn)短信驗(yàn)證的方法:
第一、實(shí)現(xiàn)php手機(jī)短信驗(yàn)證功能的基本思路
1、要找到短信服務(wù)提供商,接入短信服務(wù)
2、在網(wǎng)站信息提交頁(yè)面請(qǐng)求發(fā)送信息
3、服務(wù)器向短信服務(wù)提供商通信,提交發(fā)送請(qǐng)求
4、短信服務(wù)提供商通過(guò)運(yùn)營(yíng)商將信息發(fā)送到用戶(hù)的手機(jī)中
二、手機(jī)號(hào)碼短信驗(yàn)證前臺(tái)頁(yè)面效果實(shí)現(xiàn)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="js/jquery-1.4a2.min.js" type="text/javascript"></script> <script type="text/javascript"> /*-------------------------------------------*/ var InterValObj; //timer變量,控制時(shí)間 var count = 60; //間隔函數(shù),1秒執(zhí)行 var curCount;//當(dāng)前剩余秒數(shù) var code = ""; //驗(yàn)證碼 var codeLength = 6;//驗(yàn)證碼長(zhǎng)度 function sendMessage() { curCount = count; var dealType; //驗(yàn)證方式 tel = $(’#tel’).val(); if(tel!=’’){ //驗(yàn)證手機(jī)有效性 var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+d{8})$/; if(!myreg.test($(’#tel’).val())) { alert(’請(qǐng)輸入有效的手機(jī)號(hào)碼!’); return false; } tel = $(’#tel’).val(); //產(chǎn)生驗(yàn)證碼 for (var i = 0; i < codeLength; i++) { code += parseInt(Math.random() * 9).toString(); } //設(shè)置button效果,開(kāi)始計(jì)時(shí) $("#btnSendCode").attr("disabled", "true"); $("#btnSendCode").val("請(qǐng)?jiān)?quot; + curCount + "秒內(nèi)輸入驗(yàn)證碼"); InterValObj = window.setInterval(SetRemainTime, 1000); //啟動(dòng)計(jì)時(shí)器,1秒執(zhí)行一次 //向后臺(tái)發(fā)送處理數(shù)據(jù) $.ajax({ type: "POST", //用POST方式傳輸 dataType: "text", //數(shù)據(jù)格式:JSON url: ’yanzhengma.php’, //目標(biāo)地址(根據(jù)實(shí)際地址) data: "&tel=" + tel + "&code=" + code, error: function (XMLHttpRequest, textStatus, errorThrown) { }, success: function (msg){ } }); }else{ alert(’請(qǐng)?zhí)顚?xiě)手機(jī)號(hào)碼’); } } //timer處理函數(shù) function SetRemainTime() { if (curCount == 0) { window.clearInterval(InterValObj);//停止計(jì)時(shí)器 $("#btnSendCode").removeAttr("disabled");//啟用按鈕 $("#btnSendCode").val("重新發(fā)送驗(yàn)證碼"); code = ""; //清除驗(yàn)證碼。如果不清除,過(guò)時(shí)間后,輸入收到的驗(yàn)證碼依然有效 } else { curCount--; $("#btnSendCode").val("請(qǐng)?jiān)?quot; + curCount + "秒內(nèi)輸入驗(yàn)證碼"); } } </script> </head> <body> <input name="tel" id=tel type="text" /> <input id="btnSendCode" type="button" value="發(fā)送驗(yàn)證碼" onclick="sendMessage()" /></p> </body> </html> 第三、調(diào)用短信服務(wù)器短信接口 筆者整理的頁(yè)面是yanzhengma.php(具體根據(jù)服務(wù)商提供信息) <?php //提交短信 $post_data = array(); $post_data[’userid’] = 短信服務(wù)商提供ID; $post_data[’account’] = ’短信服務(wù)商提供用戶(hù)名’; $post_data[’password’] = ’短信服務(wù)商提供密碼’; // Session保存路徑 $sessSavePath = dirname(__FILE__)."/../data/sessions/"; if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); } session_register(’mobliecode’); $_SESSION[’mobilecode’] = $_POST["code"]; $content=’短信驗(yàn)證碼:’.$_POST["code"].’【短信驗(yàn)證】’; $post_data[’content’] = mb_convert_encoding($content,’utf-8’, ’gb2312’); //短信內(nèi)容需要用urlencode編碼下 $post_data[’mobile’] = $_POST["tel"]; $post_data[’sendtime’] = ’’; //不定時(shí)發(fā)送,值為0,定時(shí)發(fā)送,輸入格式Y(jié)YYYMMDDHHmmss的日期值 $url=’http://IP:8888/sms.aspx?action=send’; $o=’’; foreach ($post_data as $k=>$v) { $o.="$k=".$v.’&’; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要將結(jié)果直接返回到變量里,那加上這句。 $result = curl_exec($ch); ?> 第四:提交表單信息時(shí)對(duì)短信驗(yàn)證碼驗(yàn)證 //手機(jī)驗(yàn)證碼開(kāi)始 session_start(); $svalitel = $_SESSION[’mobilecode’]; $vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel)); if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’) { ResetVdValue(); //echo "Pageviews=".$vdcodetel; ShowMsg("手機(jī)驗(yàn)證碼錯(cuò)誤!", ’-1’); exit(); }
想了解