PHP function of the SMS verification code

PHP function of the SMS verification code
Now the site at the time of construction sites in order to ensure the authenticity of the user information, often choose to send text messages to mobile phone users send this code, only authenticated users can register, so we ensure that 100% of the user's contact information data accuracy resistance, but also provides users with the most convenient way to register.
Then we look at it today, SMS principle, as shown
PHP function of the SMS verification code
work development process:

 First, the basic idea php SMS verification function
  1, to find the SMS service provider, access to messaging services
  2, submit a page request information on the website information
  3, the communication server to the SMS service provider, submit a request to send

4, SMS service provider to send information to the user's mobile phone through the carrier

Second: SMS verification phone number to achieve the effect of the front page

<!DOCTYPE html>
<html lang="en">
<head>
    <title>郑州传智播客</title>
    <meta charset="UTF-8">
    <meta name="Author" content="PHP就业老师"/>
<style type="text/css">
    *{margin:0;padding:0;}
</style>
    <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">

        var InterValObj; //timer变量,控制时间

        var count = 60; //间隔函数,1秒执行

        var curCount;//当前剩余秒数

        function codeRandom(chars) {
            var res="";
            for(var i=0;i<chars;i++){
                res += Math.floor(Math.random()*10);
            }
           return res;
        }

        //timer处理函数

        function SetRemainTime() {

            if (curCount == 0) {

                window.clearInterval(InterValObj);//停止计时器

                $("#sub").removeAttr("disabled");//启用按钮

                $("#sub").val("重新发送验证码");

                code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效

            }
            else {
                curCount--;
                $("#sub").val("请在" + curCount + "秒内输入验证码");
            }

        }
        //获取手机号码
  $(function () {
    $("#sub").click(function () {
        var phone=$("#phone").val(); //获取手机号码
        curCount = count;
        if(phone!=''){

            //验证手机有效性

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

            if(!myreg.test($('#phone').val()))

            {

                alert('请输入有效的手机号码!');

                return false;

            }

            phone = $('#phone').val();

        //验证码(随机生成)
        var code= codeRandom(4);
            //设置button效果,开始计时

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

            $("#sub").val("请在" + curCount + "秒内输入验证码");

            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次

      //发送数据到后台 通过Ajax
        $.ajax({
            //设置的参数
            type: "post",
            url: "data.php",
            data:{"code":code,"phone":phone},
            success:function (msg) {
                alert(msg);
            }
        });}else{
            alert('请填写手机号码');

        }
    });
  });
    </script>
</head>
<body>
<h1>PHP-短信验证码</h1>
手机验证码:<input type="text" name="phone" id="phone">
<input id="sub" type="button" value="发送验证码" />
</body>
</html>

Third, call the SMS server SMS Interface

?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/8/30
 * Time: 14:59
 */

//$_post
$phone= isset($_POST['phone'])?$_POST['phone']:'';
$code = isset($_POST['code'])?$_POST['code']:'';
require (dirname(__FILE__).'/config.php');
require (dirname(__FILE__).'/SendSMS.php');

//实例化短信发送类
$sms= new  SendSMS($options['account'],$options['password']);
$context='验证码'.$code;
$res=$sms->send($phone,$context);
if ($res){
   echo "成功";
}else{
    echo "失败";

Because we have to code elegant, easy to write separate code reuse it later to send a short message class encapsulates specifically look at the specific code.:

<?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/8/30
 * Time: 15:26
 */
/**
 * 设置用户信息
 */
class SendSMS{
 const SENDURL='http://gd.ums86.com:8899/sms/Api/Send.do';
    private $_un;
    private $_pw;

    function __construct($user,$pwd){
        $this->_un=$user;
        $this->_pw=$pwd;
    }

    function send($phone,$content,$isreport=0){
        //发送数据
        $data=array(
            'un'=>$this->_un,
            'pw'=>$this->_pw,
            'sm'=>$content,
            'da'=>$phone,
            'rd'=>$isreport,
            'rf'=>2,
            'tf'=>3,
            'dc'=>15,

        );

        $url=SendSMS::SENDURL.'?'.http_build_query($data);
        $this->curlGet($url);

    }
   public function curlGet($url){
     $ch= curl_init();
     curl_setopt($ch,CURLOPT_HEADER,0);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch,CURLOPT_URL,$url);
     $res=curl_exec($ch);
     curl_close($ch);
     return $res;
    }
}

In some ways SendSMS inside, you example code inside the third-party verification code SMS service provider can be seen, the basic functions are similar, so we realize a class of their own to send a message, it is relatively simple.
Finally, we realized one of their own SMS sending verification code
PHP function of the SMS verification code

So finally, you learn it?

Guess you like

Origin blog.51cto.com/14473726/2431245