当前位置:网站首页>Example of SMS interface verification code function implemented by ThinkPHP framework

Example of SMS interface verification code function implemented by ThinkPHP framework

2022-06-24 12:17:00 PHP Development Engineer

This article gives an example of thinkPHP The SMS interface verification code function implemented by the framework . Share with you for your reference , As follows :

I use a model called SMS treasure Application , Newly registered users are free 3 Test SMS , Find a BUG, The same mobile phone can be registered indefinitely , It's OK to play by yourself .

There is no message in the SMS interface code , I don't feel very clear , I tested it myself , It can be used , Just call it directly , There are still a lot of details to deal with if it is used in the project , such as Set the lifetime of a verification code , And you can only press the button every other minute , There is also the ability to judge whether another user can use the SMS sent by this user , I will do it again when I have time , Now let's start with a simple version

View layer View

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Ajax No refresh upload </title>
</head>
<script type="text/javascript" src="__PUBLIC__/js/jquery-1.10.2.min.js"></script>
<body>
  <div>
     Please enter your mobile number :<input type="text" name="phone" class="phone">
  </div>
  <div>
    <a href="javascript:;" rel="external nofollow" class="butphone"> Click to get SMS verification code </a>
  </div>
</body>
  <script type="text/javascript">
    $('.butphone').click(function(){
      var phone = $('.phone').val();// Phone number 
      $.post(
        "{:U('smsbao')}",// The method of transmission 
        {phone1:phone},// Parameters 
        function (data){
          if(data == '1'){
            alert(' Input box cannot be empty ');
          }else if(data == '2'){
            alert(' The value entered is non numeric ');
          }else{
            alert(data);
          }
          },
          'json'
        )
    })
  </script>
</html>

Controller layer Controller

<?php
  namespace Home\Controller;
  use Think\Controller;
  class IndexController extends Controller 
  {
    // View 
    public function index()
    {
      $this->display();
    }
    // Short message interface  
    public function smsbao()
    {  
      $phonenum = trim( I('post.phone1') );// Parameters received 
      // First, judge that the phone is not empty 
      if(empty($phonenum) ){
        $this->ajaxReturn('1');
      }
      // Judge whether it is a number 
      $boolphone = is_numeric($phonenum);
      if($boolphone){
        $statusStr = array(
          "0"   => " Message sent successfully ",
          "-1" => " Incomplete parameters ",
          "-2" => " Server space does not support , Please confirm support curl perhaps fsocket, Contact your space provider to solve or replace the space !",
          "30" => " Wrong password ",
          "40" => " Account does not exist ",
          "41" => " Lack of balance ",
          "42" => " The account has expired ",
          "43" => "IP Address restrictions ",
          "50" => " The content contains sensitive words "
          );
          $num = rand(100000, 999999);// Set random numbers 
          setcookie("numset", $num);// Put values in COOKIE in , Close the browser and clear cookie;
          $smsapi = "http://api.smsbao.com/";
          $user = "asdf75054138"; // SMS platform account 
          $pass = md5("asdf75054138"); // SMS platform password 
          $content="[Bug], Verification from Four Musketeers , Verification Code :".$num;// The text message to send , Just set up 
          $phone = $phonenum;// Mobile phone number to send SMS 
          $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);// Fixed format 
          $result =file_get_contents($sendurl);// Fixed format 
          $this->ajaxReturn( $statusStr[$result] );// Whether the SMS is sent successfully 
        }else{
            $this->ajaxReturn( '2' );
        }
      }
  }

Various renderings :

Complete example :http://github.crmeb.net/u/defu

come from “ Open source world ” , link :https://ym.baisou.ltd/post/675.html, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .

原网站

版权声明
本文为[PHP Development Engineer ]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210603160936200W.html