1. 程式人生 > >【源碼分享】php怎樣接入短信驗證碼,對接短信驗證碼接口

【源碼分享】php怎樣接入短信驗證碼,對接短信驗證碼接口

要求 reg eth pad 收集 我們 borde value 一個

今天公司提出一個需求,要在現有項目上收集註冊用戶的真實手機號,由於之前沒有接觸過這一塊,只能尋求度娘的幫助,經過一天的努力,終於完成了,現整理記錄下已備查閱。

1 解決方案:在註冊時要求用戶進行手機驗證。
2 尋找短信供應商:由於對這一塊不是太懂,大學同學推薦一家他們公司在用的給我。
3 代碼實現
首先到互億無線短信平臺註冊一個帳號,並登錄到用戶中心,選驗證碼模塊下載接口文檔,文檔下載下來是一個壓縮包,我們項目是用PHP開發的,直接找到目錄DEMO/PHP/OOP

提示:開始之前先看一下官方對接說明文檔

第一步 把前端html代碼整合到註冊頁面
html 代碼:

<form action="index.php" method="post" name="formUser">

<table width="100%" border="0" align="left" cellpadding="5" cellspacing="3">
<tr>
<td align="right">手機</td>
<td>
<input id="mobile" name="mobile" type="text" size="25" class="inputBg" /><span style="color:#FF0000"> *</span>
</td>
</tr>
<tr>
<td align="right">驗證碼</td>
<td>
<input type="text" name="gd_code" class="inputBg" size="25" id="gd_code">
<span> <img src="code.php" onClick="javascript:this.src=this.src+‘?date=‘+Date();"></span>
</td>
</tr>
<tr>
<td align="right">手機驗證碼</td>
<td align="left">
<input type="text" name="mobile_code" class="inputBg" size="25" />
<input id="zphone" type="button" value=" 獲取手機驗證碼 " style="width: 120px" onClick="get_mobile_code()">
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value=" 註冊 " class="button"></td>
</tr>
</table>
</form>

javascript代碼

<script language="javascript">
function get_mobile_code(){
$.post(‘reg.php?send_sms=1‘, {mobile:jQuery.trim($(‘#mobile‘).val()),send_code:$("#gd_code").val()}, function(msg) {
alert(jQuery.trim(unescape(msg)));
if(msg ==‘提交成功‘){
RemainTime();
}else{
// location.reload();
}
});
};
var iTime = 59;
var Account;
function RemainTime(){
document.getElementById(‘zphone‘).disabled = true;
var iSecond,sSecond="",sTime="";
if (iTime >= 0){
iSecond = parseInt(iTime%60);
iMinute = parseInt(iTime/60)
if (iSecond >= 0){
if(iMinute>0){
sSecond = iMinute + "分" + iSecond + "秒";
}else{
sSecond = iSecond + "秒";
}
}
sTime=sSecond;
if(iTime==0){
clearTimeout(Account);
sTime=‘獲取手機驗證碼‘;
iTime = 59;
document.getElementById(‘zphone‘).disabled = false;
}else{
Account = setTimeout("RemainTime()",1000);
iTime=iTime-1;
}
}else{
sTime=‘沒有倒計時‘;
}
document.getElementById(‘zphone‘).value = sTime;
}

</script>

第一步 把後端php代碼整合到程序邏輯中。
註意:把sms.class.php、code.php、config.ihuyi.php、font.ttf這四個文件要上傳到項目路徑下

session_start();
date_default_timezone_set("PRC");

if(!empty($_GET[‘send_sms‘])){
include(‘sms.class.php‘);
$sms = new ihuyi_sms;
$sms -> send_sms($_POST[‘mobile‘],$_POST[‘send_code‘]);
die;
}

if($_POST){
if($_POST[‘mobile‘]!=$_SESSION[‘mobile‘] or $_POST[‘mobile_code‘]!=$_SESSION[‘mobile_code‘] or empty($_POST[‘mobile‘]) or empty($_POST[‘mobile_code‘])){
exit(‘手機驗證碼輸入錯誤。‘);
}else{
$_SESSION[‘mobile‘] = ‘‘;
$_SESSION[‘mobile_code‘] = ‘‘;

    //註冊邏輯

    exit(‘註冊成功。‘);
}

}

【源碼分享】php怎樣接入短信驗證碼,對接短信驗證碼接口