板鄧:php實現手機短信驗證註冊功能—轉

    如今網站在建設網站時爲了保證用戶信息的真實性,每每會選擇發短信給用戶手機發驗證碼信息,只有經過驗證的用戶才能夠註冊,這樣保證了用戶的聯繫信息資料的100%的準確性 。今天筆者就跟你們分享一下如何實現php手機短信驗證功能,但願對你們有所幫助。javascript

 

第1、實現php手機短信驗證功能的基本思路php

一、要找到短信服務提供商,接入短信服務html

二、在網站信息提交頁面請求發送信息java

三、服務器向短信服務提供商通訊,提交發送請求jquery

四、短信服務提供商經過運營商將信息發送到用戶的手機中ajax

2、手機號碼短信驗證前臺頁面效果實現服務器

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">session

<html xmlns="http://www.w3.org/1999/xhtml">dom

<head>curl

    <title></title>

    <script src="js/jquery-1.4a2.min.js" type="text/javascript"></script>

              <script type="text/javascript">

                            /*-------------------------------------------*/

                            var InterValObj; //timer變量,控制時間

                            var count = 60; //間隔函數,1秒執行

                            var curCount;//當前剩餘秒數

                            var code = ""; //驗證碼

                            var codeLength = 6;//驗證碼長度

                            function sendMessage() {

                                                 curCount = count;

                                                 var dealType; //驗證方式

                            tel = $(’#tel’).val();

                  if(tel!=’’){

                       //驗證手機有效性

                      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

            if(!myreg.test($(’#tel’).val()))

          {

             alert(’請輸入有效的手機號碼!’);

             return false;

          }

                     tel = $(’#tel’).val();

                        //產生驗證碼

                            for (var i = 0; i < codeLength; i++) {

                                                        code += parseInt(Math.random() * 9).toString();

                                                 }

                                                 //設置button效果,開始計時

                                                        $("#btnSendCode").attr("disabled", "true");

                                                        $("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");

                                                        InterValObj = window.setInterval(SetRemainTime, 1000); //啓動計時器,1秒執行一次

                            //向後臺發送處理數據

                $.ajax({

                    type: "POST", //用POST方式傳輸

                    dataType: "text", //數據格式:JSON

                    url: ’yanzhengma.php’, //目標地址(根據實際地址)

                    data: "&tel=" + tel + "&code=" + code,

                    error: function (XMLHttpRequest, textStatus, errorThrown) { },

                    success: function (msg){ }

                });

                     }else{

                     alert(’請填寫手機號碼’);

                      }

           }

                            //timer處理函數

                     function SetRemainTime() {

                                   if (curCount == 0) {                

                                          window.clearInterval(InterValObj);//中止計時器

                                          $("#btnSendCode").removeAttr("disabled");//啓用按鈕

                                          $("#btnSendCode").val("從新發送驗證碼");

                                          code = ""; //清除驗證碼。若是不清除,過期間後,輸入收到的驗證碼依然有效   

                                   }

                                   else {

                                          curCount--;

                                          $("#btnSendCode").val("請在" + curCount + "秒內輸入驗證碼");

                                   }

                            }

    </script>

</head>

<body>

<input name="tel" id=tel type="text" />

        <input id="btnSendCode" type="button" value="發送驗證碼" onclick="sendMessage()" /></p>

</body>

</html>

第3、調用短信服務器短信接口

筆者整理的頁面是yanzhengma.php(具體根據服務商提供信息)

<?php //提交短信

$post_data = array();

$post_data[’userid’] = 短信服務商提供ID;

$post_data[’account’] = ’短信服務商提供用戶名’;

$post_data[’password’] = ’短信服務商提供密碼’;

// Session保存路徑

$sessSavePath = dirname(__FILE__)."/../data/sessions/";

if(is_writeable($sessSavePath) && is_readable($sessSavePath)){

       session_save_path($sessSavePath);

}

session_register(’mobliecode’);

$_SESSION[’mobilecode’] = $_POST["code"];

$content=’短信驗證碼:’.$_POST["code"].’【短信驗證】’;

$post_data[’content’] = mb_convert_encoding($content,’utf-8’, ’gb2312’); //短信內容須要用urlencode編碼下

$post_data[’mobile’] = $_POST["tel"];

$post_data[’sendtime’] = ’’; //不定時發送,值爲0,定時發送,輸入格式YYYYMMDDHHmmss的日期值

$url=’http://IP:8888/sms.aspx?action=send’;

$o=’’;

foreach ($post_data as $k=>$v)

{

  $o.="$k=".$v.’&’;

}

$post_data=substr($o,0,-1);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_URL,$url);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //若是須要將結果直接返回到變量裏,那加上這句。

$result = curl_exec($ch);

?>

第四:提交表單信息時對短信驗證碼驗證

//手機驗證碼開始

        session_start();

              $svalitel = $_SESSION[’mobilecode’];

              $vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel));

       

            if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’)

            {

                ResetVdValue();

                            //echo "Pageviews=".$vdcodetel;

                            ShowMsg("手機驗證碼錯誤!", ’-1’);

                exit();

            }

相關文章
相關標籤/搜索