溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php實(shí)現(xiàn)短信驗(yàn)證的方法

發(fā)布時(shí)間:2020-09-30 14:33:15 來源:億速云 閱讀:167 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)php實(shí)現(xiàn)短信驗(yàn)證的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

php實(shí)現(xiàn)短信驗(yàn)證的方法:首先接入短信服務(wù);然后在網(wǎng)站信息提交頁面請(qǐng)求發(fā)送信息;接著服務(wù)器向短信服務(wù)提供商通信,提交發(fā)送請(qǐng)求;最后短信服務(wù)提供商通過運(yùn)營商將信息發(fā)送到用戶的手機(jī)中。

php實(shí)現(xiàn)短信驗(yàn)證的方法:

第一、實(shí)現(xiàn)php手機(jī)短信驗(yàn)證功能的基本思路

1、要找到短信服務(wù)提供商,接入短信服務(wù)

2、在網(wǎng)站信息提交頁面請(qǐng)求發(fā)送信息

3、服務(wù)器向短信服務(wù)提供商通信,提交發(fā)送請(qǐng)求

4、短信服務(wù)提供商通過運(yùn)營商將信息發(fā)送到用戶的手機(jī)中

二、手機(jī)號(hào)碼短信驗(yàn)證前臺(tái)頁面效果實(shí)現(xiàn)

php實(shí)現(xiàn)短信驗(yàn)證的方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="js/jquery-1.4a2.min.js" type="text/javascript"></script>
              <script type="text/javascript">
                            /*-------------------------------------------*/
                            var InterValObj; //timer變量,控制時(shí)間
                            var count = 60; //間隔函數(shù),1秒執(zhí)行
                            var curCount;//當(dāng)前剩余秒數(shù)
                            var code = ""; //驗(yàn)證碼
                            var codeLength = 6;//驗(yàn)證碼長度
                            function sendMessage() {
                                                 curCount = count;
                                                 var dealType; //驗(yàn)證方式
                            tel = $(’#tel’).val();
                  if(tel!=’’){
                       //驗(yàn)證手機(jī)有效性
                      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if(!myreg.test($(’#tel’).val()))
          {
             alert(’請(qǐng)輸入有效的手機(jī)號(hào)碼!’);
             return false;
          }
                     tel = $(’#tel’).val();
                        //產(chǎn)生驗(yàn)證碼
                            for (var i = 0; i < codeLength; i++) {
                                                        code += parseInt(Math.random() * 9).toString();
                                                 }
                                                 //設(shè)置button效果,開始計(jì)時(shí)
                                                        $("#btnSendCode").attr("disabled", "true");
                                                        $("#btnSendCode").val("請(qǐng)?jiān)?quot; + curCount + "秒內(nèi)輸入驗(yàn)證碼");
                                                        InterValObj = window.setInterval(SetRemainTime, 1000); //啟動(dòng)計(jì)時(shí)器,1秒執(zhí)行一次
                            //向后臺(tái)發(fā)送處理數(shù)據(jù)
                $.ajax({
                    type: "POST", //用POST方式傳輸
                    dataType: "text", //數(shù)據(jù)格式:JSON
                    url: ’yanzhengma.php’, //目標(biāo)地址(根據(jù)實(shí)際地址)
                    data: "&tel=" + tel + "&code=" + code,
                    error: function (XMLHttpRequest, textStatus, errorThrown) { },
                    success: function (msg){ }
                });
                     }else{
                     alert(’請(qǐng)?zhí)顚懯謾C(jī)號(hào)碼’);
                      }
           }
                            //timer處理函數(shù)
                     function SetRemainTime() {
                                   if (curCount == 0) {
                                          window.clearInterval(InterValObj);//停止計(jì)時(shí)器
                                          $("#btnSendCode").removeAttr("disabled");//啟用按鈕
                                          $("#btnSendCode").val("重新發(fā)送驗(yàn)證碼");
                                          code = ""; //清除驗(yàn)證碼。如果不清除,過時(shí)間后,輸入收到的驗(yàn)證碼依然有效
                                   }
                                   else {
                                          curCount--;
                                          $("#btnSendCode").val("請(qǐng)?jiān)?quot; + curCount + "秒內(nèi)輸入驗(yàn)證碼");
                                   }
                            }
    </script>
</head>
<body>
<input name="tel" id=tel type="text" />
        <input id="btnSendCode" type="button" value="發(fā)送驗(yàn)證碼" onclick="sendMessage()" /></p>
</body>
</html>
第三、調(diào)用短信服務(wù)器短信接口
筆者整理的頁面是yanzhengma.php(具體根據(jù)服務(wù)商提供信息)
<?php //提交短信
$post_data = array();
$post_data[’userid’] = 短信服務(wù)商提供ID;
$post_data[’account’] = ’短信服務(wù)商提供用戶名’;
$post_data[’password’] = ’短信服務(wù)商提供密碼’;
// 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=’短信驗(yàn)證碼:’.$_POST["code"].’【短信驗(yàn)證】’;
$post_data[’content’] = mb_convert_encoding($content,’utf-8’, ’gb2312’); //短信內(nèi)容需要用urlencode編碼下
$post_data[’mobile’] = $_POST["tel"];
$post_data[’sendtime’] = ’’; //不定時(shí)發(fā)送,值為0,定時(shí)發(fā)送,輸入格式Y(jié)YYYMMDDHHmmss的日期值
$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); //如果需要將結(jié)果直接返回到變量里,那加上這句。
$result = curl_exec($ch);
?>
第四:提交表單信息時(shí)對(duì)短信驗(yàn)證碼驗(yàn)證
//手機(jī)驗(yàn)證碼開始
        session_start();
              $svalitel = $_SESSION[’mobilecode’];
              $vdcodetel = empty($vdcodetel) ? ’’ : strtolower(trim($vdcodetel));
            if(strtolower($vdcodetel)!=$svalitel || $svalitel==’’)
            {
                ResetVdValue();
                            //echo "Pageviews=".$vdcodetel;
                            ShowMsg("手機(jī)驗(yàn)證碼錯(cuò)誤!", ’-1’);
                exit();
            }

關(guān)于php實(shí)現(xiàn)短信驗(yàn)證的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI