SMS Authentication Code Function in PHP


In order to ensure the authenticity of users'information, websites often choose to send text messages to users' mobile phones to send verification code information. Only authenticated users can register. This not only ensures 100% accuracy of users'contact information, but also provides users with the most convenient and fast way to register.
So today let's talk about how text messaging works, like the following

Work development process:

First, the basic idea of realizing the verification function of php mobile SMS
1. To find the SMS service provider, access the SMS service
2. Request to send information on the site information submission page
3. Server communicates with SMS service provider and submits send request

4. SMS service providers send information to users'mobile phones through operators

Second: the effect of mobile phone number SMS verification foreground page

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Zhengzhou Chuanzhi Podcast</title>
    <meta charset="UTF-8">
    <meta name="Author" content="PHP Employment Teachers"/>
<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 variable, controlling time

        var count = 60; //Interval function, 1 second execution

        var curCount;//Current remaining seconds

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

        //timer handler

        function SetRemainTime() {

            if (curCount == 0) {

                window.clearInterval(InterValObj);//Stop timer

                $("#Sub'). removeAttr ('disabled'); //Enable button

                $("#Sub').val ('Resend Authentication Code');

                code = ""; //Clear the verification code.If not cleared, after time, the input received Authentication Code is still valid

            }
            else {
                curCount--;
                $("#Sub').val ('Please enter the verification code in seconds'+curCount +');
            }

        }
        //Get your mobile number
  $(function () {
    $("#sub").click(function () {
        var phone=$("#Phone').val(); //Get your mobile number
        curCount = count;
        if(phone!=''){

            //Verify the effectiveness of your mobile phone

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

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

            {

                alert('Please enter a valid mobile number!');

                return false;

            }

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

        //Verification Code (Random Generation)
        var code= codeRandom(4);
            //Set button effect, start timing

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

            $("#Sub').val ('Please enter the verification code in seconds'+curCount +');

            InterValObj = window.setInterval(SetRemainTime, 1000); //Start the timer and execute once a second

      //Send data to the background via Ajax
        $.ajax({
            //Set parameters
            type: "post",
            url: "data.php",
            data:{"code":code,"phone":phone},
            success:function (msg) {
                alert(msg);
            }
        });}else{
            alert('Please fill in your mobile number');

        }
    });
  });
    </script>
</head>
<body>
<h1>PHP-SMS Authentication Code</h1>
//Mobile phone verification code: <input type="text" name="phone" id="phone">
<input id="sub" type="button" value="Send Authentication Code" />
</body>
</html>

Third, call SMS interface of SMS server

?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');

//Instantiate SMS Sending Class
$sms= new  SendSMS($options['account'],$options['password']);
$context='Verification Code'.$code;
$res=$sms->send($phone,$context);
if ($res){
   echo "Success";
}else{
    echo "fail";

Because we write code separately for the sake of code elegance and reuse it later. So we encapsulate sending short messages in a specific class. See code specifically:

<?php
/**
 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/8/30
 * Time: 15:26
 */
/**
 * Set User Information
 */
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){
        //send data
        $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;
    }
}

Some methods in EndSMS, you can see in the sample code provided by third-party SMS Authentication Code service providers, the functions are basically the same, so we have to implement our own sending message class, which is simpler.
Finally, we have implemented a SMS Authentication Code sending function of our own.

So in the end, did you learn?

Keywords: PHP Mobile Javascript PhpStorm

Added by videxx on Wed, 21 Aug 2019 05:17:31 +0300