來源URL:http://www.yunda51.com/1077.html
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; } } ?>
<!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>
