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">
    <title>Zhengzhou Chuanzhi Podcast</title>
    <meta charset="UTF-8">
    <meta name="Author" content="PHP Employment Teachers"/>
<style type="text/css">
    <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 {
                $("#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;

            //Verify the effectiveness of your mobile phone

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



                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
            //Set parameters
            type: "post",
            url: "data.php",
            success:function (msg) {
            alert('Please fill in your mobile number');

<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" />

Third, call SMS interface of SMS server

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

$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;
if ($res){
   echo "Success";
    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:

 * Created by PhpStorm.
 * User: Leo
 * Date: 2017/8/30
 * Time: 15:26
 * Set User Information
class SendSMS{
 const SENDURL='';
    private $_un;
    private $_pw;

    function __construct($user,$pwd){

    function send($phone,$content,$isreport=0){
        //send data



   public function curlGet($url){
     $ch= curl_init();
     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