PHP implementation of the Ali SMS Verification Method code

Source: Internet
Author: User
Tags sendmsg
Recently want to implement PHP to achieve the effect of SMS verification, when doing a PC website, can be registered users need to use the function of SMS authentication, or retrieve the password, as well as verify the user's information and so on. This article mainly and introduce PHP to realize Ali Big fish Short message verification of the case code of the relevant data, with a certain reference value, interested in small partners can refer to, hope to help everyone.

The first step

Login Ali is larger than registered account, create application in User Management Center, determine appkey and app secret and configure signature

Step Two

Select the SDK download in app management, or click http://www.jb51.net/softs/312325.html to download it for free, OH pro

Unzip the downloaded resources and put them in the Thinkphp\library\vendor directory.

Not all PHP files are required, just use the PHP files under the Alidayu folder to open the PHP files inside the Alidayu, and add the code on the first line (if you download the link from me, there is no need to add this code, because I added)


namespace Vendor\alidayu;

The goal is to not introduce failures at the time of the call.

Step Three

You can write HTML.


<p class= "Form-group" > <p class= "field field-icon-right" >  <input type= "text" id= "admin" class= " Input "name=" admin "placeholder=" User name "data-validate=" required: Please fill in the user name, length#>=5: User length does not meet the requirements "/>  <span class= "icon Icon-user" ></span> </p></p>


<p class= "Form-group" > <p class= "field field-icon-right" >  <input id= "mobile" type= "Tel" class= " Input "name=" mobile "placeholder=" phone number "onblur=" checkmobile (This) "/>  <span class=" icon Icon-mobile "> </span> </p></p>


<button id= "sendmsg" > Get Verification Code </button>

Fourth Step

Write the JQ code (function is to send a verification code after the countdown, and post delivery background, the background returned data 0,1,2 respectively corresponding to different functions) This code is reference to other people's


<script >/*-------------------------------------------*/var intervalobj; Timer variable, control time var count = 60; Interval function, 1 seconds to execute var curcount;//current remaining seconds var code = ""; Verification code var codelength = 6;//Verification Code length $ (function () {$ (' #sendmsg '). Click (function () {$.ajax ({type: "POST", url: "/ User/folder/child/obtainyzm ", Data:" admin= "+ $ (' #admin '). Val () +" &mobile= "+$ (" #mobile "). Val (), Success:functi      On (Result) {if (result==0) {curcount = count;      Set the button effect to start timing $ ("#sendmsg"). CSS ("Background-color", "Lightskyblue");      $ ("#sendmsg"). attr ("Disabled", "true");      $ ("#sendmsg"). Val ("get" + Curcount + "SEC"); Intervalobj = Window.setinterval (setremaintime, 1000);     Start timer, 1 seconds to execute//alert ("Verification code sent successfully, please check!");     } if (result==1) {alert ("username and phone number does not match!"); } if (result==2) {alert ("User name does not exist!)     "); }, DataType: ' JSON '})}) function Setremaintime () {if (Curcount = = 0) {window.clearinterval (Intervalob j);//Stop Timer $ ("#sendmsg"). RemovEattr ("Disabled");//Enable button $ ("#sendmsg"). CSS ("Background-color", "" ");   $ ("#sendmsg"). Val ("re-issued verification Code"); Code = ""; Clear the Verification code.   If not cleared, after the time, the input received the verification code is still valid} else {curcount--;  $ ("#sendmsg"). Val ("get" + Curcount + "SEC"); }}</script>

There are two parameters, admin and Mobile, which represent the user name and mobile number respectively.

Fifth Step

Write function code


Public Function Obtainyzm () {$mobile = $_post[' mobile '];//get the phone number $admin =$_post[' admin '];//get the user name $user = M (' db_admin ')-& Gt;where (Array (' admin ' = $admin))->find (); /************ introduced *************/Vendor (' alidayu.topclient '); Vendor (' alidayu.alibabaaliqinfcsmsnumsendrequest '); Vendor (' Alidayu.resultset '); Vendor (' Alidayu.requestcheckutil '); $c = new \vendor\alidayu\topclient; $req = new \vendor\alidayu\alibabaaliqinfcsmsnumsendrequest; /************* configuration ***************/$code = Randcode (4);//Random verification code $c->appkey = ' 23****** '; $c->secretkey = ' 6f73a****************** '; $req->setsmstype ("normal"); $req->setsmsfreesignname ("Test of XX"); $req->setsmsparam ("{code: ' $code '}"); $req->setrecnum ("$mobile"); $req->setsmstemplatecode ("sms_3******"); if ($user) {if ($user [' mobile '] = = $mobile) {/************* sends a CAPTCHA SMS and saves the verification code as a new password to the server ***************///$c->execut E ($req); Do not open, after the opening will have text messages to the account, a few cents.   It's good to see the new password on the server $newpwd [' pwd '] = MD5 ($code); D (' Db_admin ')->where (Array (' admin ' => $user [' admin ']))->save ($NEWPWD);  $this->ajaxreturn (0);//user name password match} else {$this->ajaxreturn (1);//username and phone number does not match}} else {$this->ajaxreturn (2); User name does not exist} $this->display ();} Get Verification Code
Related Article

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.