《PHP學習:PHP實現阿里大魚短信驗證的實例代碼》要點:
本文介紹了PHP學習:PHP實現阿里大魚短信驗證的實例代碼,希望對您有用。如果有疑問,可以聯系我們。
PHP編程最近想實現PHP實現短信驗證的效果,做PC網站的時候,可以通過注冊用戶需要使用短信驗證的功能,或者找回密碼,以及驗證用戶的信息等等功能,發現了阿里大魚.留個筆記
PHP編程第一步
PHP編程登陸阿里大于注冊賬號,在用戶管理中心創建應用,確定AppKEY和App Secret還有配置簽名
PHP編程
PHP編程
PHP編程第二步
PHP編程在應用管理中選擇SDK下載,或者直接點擊/softs/312325.html下載,免費的哦親
PHP編程把下載好的資源解壓放到ThinkPHP\Library\Vendor目錄下如圖
PHP編程
PHP編程并不是所有的php文件都需要,就用如圖所示Alidayu文件夾下那幾個php文件,把Alidayu里面的php文件分別打開,在開始的一行添加代碼(如果你下載的來自我發的鏈接,那就沒必要添加這個代碼了,因為我添加過了)
PHP編程
namespace Vendor\Alidayu;
PHP編程目的是為了在調用的時候不至于引入失敗
PHP編程第三步
PHP編程可以寫HTML了
PHP編程
<div class="form-group">
<div class="field field-icon-right">
<input type="text" id="admin" class="input" name="admin" placeholder="用戶名" data-validate="required:請填寫用戶名,length#>=5:用戶長度不符合要求" />
<span class="icon icon-user"></span>
</div>
</div>
PHP編程
<div class="form-group">
<div class="field field-icon-right">
<input id="mobile" type="tel" class="input" name="mobile" placeholder="手機號碼" onblur="checkmobile(this)" />
<span class="icon icon-mobile"></span>
</div>
</div>
PHP編程
<button id="sendmsg">獲取驗證碼</button>
PHP編程第四步
PHP編程寫入JQ代碼(功能是,發送驗證碼后倒計時,并且POST傳遞后臺,后臺返回的數據0,1,2分別對應不同的功能)這段代碼是參考別人的
PHP編程
<script >
/*-------------------------------------------*/
var InterValObj; //timer變量,控制時間
var count = 60; //間隔函數,1秒執行
var curCount;//當前剩余秒數
var code = ""; //驗證碼
var codeLength = 6;//驗證碼長度
$(function () {
$('#sendmsg').click(function () {
$.ajax({
type: "POST",
url: "/User/folder/child/obtainyzm",
data: "admin=" + $('#admin').val()+"&mobile="+$("#mobile").val() ,
success: function (result) {
if(result==0){
curCount = count;
//設置button效果,開始計時
$("#sendmsg").css("background-color", "LightSkyBlue");
$("#sendmsg").attr("disabled", "true");
$("#sendmsg").val("獲取" + curCount + "秒");
InterValObj = window.setInterval(SetRemainTime, 1000); //啟動計時器,1秒執行一次
// alert("驗證碼發送成功,請查收!");
}
if(result==1){
alert("用戶名和手機號不匹配!");
}
if(result==2){
alert("用戶名不存在!");
}
},
dataType: 'json'
})
})
})
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止計時器
$("#sendmsg").removeAttr("disabled");//啟用按鈕
$("#sendmsg").css("background-color", "");
$("#sendmsg").val("重發驗證碼");
code = ""; //清除驗證碼.如果不清除,過時間后,輸入收到的驗證碼依然有效
}
else {
curCount--;
$("#sendmsg").val("獲取" + curCount + "秒");
}
}
</script>
PHP編程上面有兩個參數admin和mobile分別代表用戶名和手機號碼.
PHP編程第五步
PHP編程寫入功能代碼
PHP編程
public function obtainyzm(){
$mobile = $_POST['mobile'];//獲取手機號碼
$admin =$_POST['admin'];//獲取用戶名
$user = M('db_admin')->where(array('admin'=>$admin))->find();
/************引入*************/
Vendor('Alidayu.TopClient');
Vendor('Alidayu.AlibabaAliqinFcSmsNumSendRequest');
Vendor('Alidayu.ResultSet');
Vendor('Alidayu.RequestCheckUtil');
$c = new \Vendor\Alidayu\TopClient;
$req = new \Vendor\Alidayu\AlibabaAliqinFcSmsNumSendRequest;
/*************配置***************/
$code = randCode(4);//隨機驗證碼
$c->appkey = '23******';
$c->secretKey = '6f73a******************';
$req->setSmsType("normal");
$req->setSmsFreeSignName("xx的測試");
$req->setSmsParam("{code:'$code'}");
$req->setRecNum("$mobile");
$req->setSmsTemplateCode("SMS_3******");
if($user)
{
if($user['mobile'] == $mobile)
{
/*************發送驗證碼短信,并把驗證碼作為新密碼保存到服務器上***************/
//$c->execute($req); //不要開啟,開啟后就會有短信到賬,一次幾分錢..在服務器上看新密碼就好
$newpwd['pwd'] = md5($code);
D('db_admin')->where(array('admin'=>$user['admin']))->save($newpwd);
$this->ajaxreturn(0);//用戶名密碼匹配
}
else
{
$this->ajaxreturn(1);//用戶名和手機號不匹配
}
}
else
{
$this->ajaxreturn(2); //用戶名不存在
}
$this->display();
}//獲取驗證碼
PHP編程以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/502.html