現(xiàn)在很多網(wǎng)站用戶注冊頁面都添加有短信驗證碼功能,下面就來用實(shí)例為大家介紹下如何實(shí)現(xiàn)網(wǎng)站短信驗證碼注冊功能。
前端用戶注冊頁面效果:
前端注冊頁面代碼:
<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('驗證碼正確,請繼續(xù)!'); } 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 = "再次發(fā)送"; 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("驗證信息會發(fā)送到"+$("#mobile").val()); test.init(btn); }; }); </script> </HEAD> <BODY> <p>手機(jī)號:<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>
后端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中 ////如果得到手機(jī)號 if($mobile) { // echo "2";//得到手機(jī)號 // echo $_SESSION['mcode'];//測試得到的驗證碼 // echo '<br/>'; if((strtotime($_SESSION['time'])+60)<time()) {//將獲取的緩存時間轉(zhuǎn)換成時間戳加上60秒后與當(dāng)前時間比較,小于當(dāng)前時間即為過期 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['accName'] = "test";//用戶名 $post_data['accPwd'] = "test";//密碼 $post_data['aimcodes'] = $mobile;//手機(jī)號,多個號碼以分號分隔,如:13407100000;13407100001;13407100002 $post_data['content'] = urlencode("您本次的驗證碼是:".$mcode);//內(nèi)容,如為中文一定要使用一下urlencode函數(shù) $post_data['extno'] = "";//擴(kuò)展號,可選 $post_data['seed'] = "";//發(fā)送時間,格式:yyyy-MM-dd HH:mm:ss,可選 $url='http://www.iium.cn/send2'; //動力思維樂信短信驗證碼接口調(diào)用地址 $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);//返回相應(yīng)的標(biāo)識,動力思維樂信標(biāo)識說明:http://www.iium.cn/zxztm.html curl_close($ch); // echo $result; } } ?>
相關(guān)閱讀:
動力思維樂信短信接口API文檔:http://www.iium.cn/apitext.html
動力思維樂信php短信接口demo:http://www.iium.cn/phpcode.html