Ajax and PHP Small talk (mobile phone verification code)

Source: Internet
Author: User

In the project encountered a write phone verification code problem, so write it out:

This is the previous page, using AJAX to send a cell phone number in JSON format past

<script src= "Jquery-1.11.1.min.js"  type= "Text/javascript" ></script>  < Script type= "Text/javascript" >  var InterValObj;        //timer variable, control time   var count = 120;          //interval function, 1 seconds to execute   var curcount;//current remaining seconds   var code =  "";  // Verification Code   var codelength = 4;//Verification Code length   function sendmessage ()  {       curcount = count;      var phone=$ ( "#phone"). Val ();//Mobile number       if (phone !=  "") {           //Set button effect, start timing            $ ("#btnSendCode"). attr ("Disabled",  "true");           $ ("#btnSendCode"). Val ("Please" in " + curCount +  "Enter the verification code in seconds");           Intervalobj = window.setinterval (setremaintime, 1000);  //start timer, 1 seconds to execute        //send processing data to the background      for  (var i = 0; i  < codelength; i++)  {        code +=  parseint (Math.random ()  * 9). ToString ();        }          $.ajax ({               type:  "POST",  //post mode                dataType:  "JSON",  //data format:json               url:  ' http://192.168.1.100/cancertool/index.php?g= home&m=api&a=send                 _phone ',  //target address               data: {"Phone":p hone} ,             //error: function  ( Xmlhttprequest, textstatus, errorthrown)  { },               success: function (JSON) {                 /*if (json.success==1) {                     alert ("sent successfully");                 }else{                      alert ("Send Failed");                  return false;} */                                 alert (Json.code);             }           });      }else{           alert ("Mobile phone number cannot be empty!") ");       }  }  //timer processing function   function  Setremaintime ()  {      if  (curcount == 0)  {                            window.clearinterval (intervalobj);//Stop Timer      & NBsp;    $ ("#btnSendCode"). Removeattr ("Disabled");//Enable button            $ ("#btnSendCode"). Val ("Resend Verification Code");           code =  "";  //clear the Verification code. If not cleared, after the time, enter the verification code received is still valid           }       else {          curCount--;           $ ("#btnSendCode"). Val ("Please  + curCount + " Enter the verification code in Seconds ");       }  }  </script>  
  

At the receiving end, I am using the TP framework, receiving and returning a random code:

Public Function Send_phone () {Import (' org.msgsend '); $phone = $this->_post (' phone '); $msgsend =new msgsend (); $code = $msgsend->random ();//Generate random number $msg= ' Your verification code is: '. '            $code ";//$res = $msgsend->sendmsg ($phone, $msg);                if (Isset ($msg)) {$this->data[' code ']= $code; $this->ajaxreturn ($this->data, ' JSON '); $this->ajaxreturn ($this->data, ' JSON ');}                else{$this->data[' code ']=0;    $this->ajaxreturn ($this->data, ' JSON ');} }

Msgsend class:

<?php/* * */class msgsend{const msg_username= '; const msg_password= '; const msg_url= '; Const ramdom_code= ' 1234567890 '; const CODE_LENGTH = 4;public function __construct () {}public function sendmsg ($sendto, $msg) {$msg = Rawurlencode ($msg);//return file_get_contents ();} Public Function random () {return substr (Str_shuffle (Self::ramdom_code), 0,self::code_length);}}? >

Test, return a sequence of randomly generated codes:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/25/wKiom1R5ciyDh2Y_AAC2lg2WgHI043.jpg "title=" Qq20141129151146.png "alt=" Wkiom1r5ciydh2y_aac2lg2wghi043.jpg "/>

This article from "Clear Water neon Fog" blog, declined reprint!

Ajax and PHP Small talk (mobile phone verification code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.