手機註冊獲取驗證碼的PHP程式碼
阿新 • • 發佈:2019-02-16
手機獲取註冊驗證碼 相信很多都會遇到吧
最近測試一個手機獲取驗證碼並驗證資訊是否正確,在本地就做了一個測試,可以直接獲取使用者輸入的手機號,傳送簡訊驗證碼到使用者手機,然後使用者輸入驗證碼之後提交驗證資訊是否正確
php程式碼
<?php
require dirname(__FILE__).'/include/common.inc.php';//這是在cms2008下面做的測試
header("content-type:text/html; charset=utf-8;");
session_start();//開啟快取
if (isset($_SESSION['time']))//判斷快取時間
{
session_id();
$_SESSION['time'];
}
else
{
$_SESSION['time'] = date("Y-m-d H:i:s");
}
$_SESSION['mcode']=$_GET['mcode'];//將content的值儲存在session中
////如果得到手機號
if($mobile) {
// echo "2";//得到手機號
// echo $_SESSION['mcode'];//測試得到的驗證碼
// echo '<br/>';
if((strtotime($_SESSION ['time'])+60)<time()) {//將獲取的快取時間轉換成時間戳加上60秒後與當前時間比較,小於當前時間即為過期
session_destroy();
unset($_SESSION);
header('content-type:text/html; charset=utf-8;');
echo '<script>alert("驗證碼已過期,請重新獲取!");</script>';
}
else{
$mcode=$_SESSION['mcode'];
$post_data = array();
$post_data['username'] = "test";//使用者名稱
$post_data['password'] = "test";//密碼
$post_data['mobile'] = $mobile;//手機號,多個號碼以分號分隔,如:13407100000;13407100001;13407100002
$post_data['content'] = urlencode("您本次的驗證碼是:".$mcode);//內容,如為中文一定要使用一下urlencode函式
$post_data['extcode'] = "";//擴充套件號,可選
$post_data['senddate'] = "";//傳送時間,格式:yyyy-MM-dd HH:mm:ss,可選
$post_data['batchID'] = "";//批次號,可選
$url='http://116.213.72.20/SMSHttpService/send.aspx';
$o="";
foreach ($post_data as $k=>$v)
{
$o.= "$k=".$v."&";
}
$post_data=substr($o,0,-1);
$this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$this_header);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);//返回相應的標識,具體請參考我方提供的簡訊API文件
curl_close($ch);
// echo $result;
}
}
?>
html程式碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script src="images/js/jquery.min.js"></script>
<script src="images/js/check.js"></script>
<script>
$(document).ready(function(){
$("#Submit").click(function get_mobile(){
var mcode=Math.round(Math.random()*10000);
$.get("index.php?mobile="+$("#mobile").val()+"&mcode="+mcode,function(data){
// alert(data);
});
$("#yanzheng").click(function get_code(){
$.get("index.php?code="+$("#code").val(),function(data){
// alert(mcode);
if (mcode==$("#code").val())
{
alert('驗證碼正確,請繼續!');
}
else{
alert('驗證碼錯誤');
}
});
});
});
var test = {
node:null,
count:60,
start:function(){
//console.log(this.count);
if(this.count > 0){
this.node.innerHTML = this.count--;
var _this = this;
setTimeout(function(){
_this.start();
},1000);
}else{
this.node.removeAttribute("disabled");
this.node.innerHTML = "再次傳送";
this.count = 60;
}
},
//初始化
init:function(node){
this.node = node;
this.node.setAttribute("disabled",true);
this.start();
}
};
var btn = document.getElementById("Submit");
btn.onclick = function(){
alert("驗證資訊會發送到"+$("#mobile").val());
test.init(btn);
};
});
</script>
</HEAD>
<BODY>
<p>手機號:<input type="text" name="mobile" value="" id="mobile" onblur="check_mobile(this.value)"/><span id="mobile_notice"></span></p>
<p>驗證碼:<input type="text" name="code" value="" id="code"/><button id="Submit">獲取驗證碼</button></p>
<input type="submit" name="yanzheng" value="下一步" id="yanzheng" />
</BODY>
</HTML>