Think dream php-Ali Big Fish mobile phone verification code

Source: Internet
Author: User

When the small partner does the PC website, whether has encountered the registration user needs to use the SMS verification function? Or retrieve the password, and verify the user's information and so on functions! Today, think of PHP for everyone to bring thinkphp integrated Ali Big Fish SMS verification function!

First of all, we have to understand what the principle of text messaging, in fact, we use the third party is a communication of the SMS protocol, and then we randomly generated a verification code sent to the user's mobile phone, and then we send in the database to save the corresponding information to verify, the principle is this, We will begin to teach you to play this function step by step!

(1) We're going to build a data table that keeps text messages.

DROP TABLE IF EXISTS ' Tb_record ';

CREATE TABLE ' Tb_record ' (' id ' int (one) not NULL auto_increment, ' tel ' varchar (255) NOT null COMMENT ' mobile number ', ' VC ' varchar (2 Not NULL, ' time ' varchar (255) is not NULL, ' ctime ' varchar (255) NOT null COMMENT ' last Send verification code ', PRIMARY KEY (' id ')) ENGI Ne=myisam auto_increment=2 DEFAULT charset=utf8 comment= ' SMS Send record form ';

(2) We are going to use third-party class files, of course, this kind of file you can go to the platform download, there is PHP demo, and then we integrate into the thinkphp inside the line, join in the class library do not forget to declare the namespace!

(3) We configure some auxiliary variables in the configuration file

' url ' = ' http://www.etuocloud.com/gatetest.action ',//This is the call address of the test, after the upper limit to change ' app_key ' = ' xxxxxxxxxx ',//Key, After you apply to create the app will get ' app_secret ' + ' xxxxxxxxxxxx ',//key, after you apply to create the application will get ' format ' + ' json ',//return value formats, divided into XML and JSON

(4) This is where we start to write a logic operation.

$_post[' tel '] = "18522713XXX"; $tel = $_post[' Tel '); $w [' tel '] = $tel; $res 1 = D ("Record")->where ($w)->find ();// There can be only three chances in a day (verify) $gap = Time ()-$res 1[' CTime '];if ($gap >=86400) {$data [' time '] = 0; D ("Record")->where ($w)->save ($data);} $res 1 = D ("Record")->where ($w)->find (); if ($res 1[' time ']>=3) {echo "SMS limit!) ";} else {//The value of the randomly sent verification code $num = rand (1000, 9999);//Instantiate an object $ECD = new ECD (c (' url '), C (' App_key '), C (' App_secret '), C (' format ') ); Send verification Code SMS//The first parameter is the phone number, the second is the ID of the template, in the application management has a template management, the third is we send a random verification code, the fourth is the order number, 咟? Do not fill $res = $ecd->send_sms_code ("$tel", ' 1 ', "$num", "); $arr = Json_decode ($res, true); if ($arr [' result '] = = 0 && $arr [' msg '] = = "Success") {$w [' tel '] = $tel; $res 1 = D ("Record")->where ($w)->find (); if ($res 1) {$data [' vc '] = $num; $data [' time '] = $res 1[' time ' + 1; $data [' ctime '] = time (); $res 2 = D ("Record")->where ($w)->save ($data); if ($res 2) {echo "sent successfully";} else {echo "Send Failed";}} else {$data [' tel '] = $tel; $data [' vc '] = $num; $data [' time '] = 1; $data [' ctime '] = time (); $res 3 = D ("Record")->add ($data); if ($res 3) {echo "sent successfully";} else {echo "Send Failed";}} }}

(5) That is to receive the text message, and then the user submits the time we according to the user's mobile phone number to check the relevant verification code OK!

$_post[' tel '] = "18522713XXX";

$tel = $_post[' Tel ']; $w [' tel '] = $tel;

$res 1 = D ("Record")->where ($w)->find ();//Only three opportunities per day (verified)

$gap = Time ()-$res 1[' CTime ');

if ($gap >=86400) {

$data [' time '] = 0;

D ("Record")->where ($w)->save ($data);

}

$res 1 = D ("Record")->where ($w)->find ();

if ($res 1[' time ']>=3) {

echo "SMS limit! ";

}else {

The value of the randomly sent verification code $num = rand (1000, 9999);

Instantiate an Object

$ECD = new ECD (c (' url '), C (' App_key '), C (' App_secret '), C (' format '));

Send verification Code SMS//The first parameter is the phone number, the second is the ID of the template, in the application management has a template management, the third is we send a random verification code, the fourth is the order number, 咟?

$res = $ecd->send_sms_code ("$tel", ' 1 ', "$num", ");

$arr = Json_decode ($res, true);

if ($arr [' result '] = = 0 && $arr [' msg '] = = "Success") {

$w [' tel '] = $tel;

$res 1 = D ("Record")->where ($w)->find ();

if ($res 1) {$data [' vc '] = $num;

$data [' time '] = $res 1[' time ' + 1;

$data [' ctime '] = time ();

$res 2 = D ("Record")->where ($w)->save ($data);

if ($res 2) {

echo "Send Success";

} else {echo "Send failed";

}

} else {$data [' tel '] = $tel;

$data [' vc '] = $num;

$data [' time '] = 1;

$data [' ctime '] = time ();

$res 3 = D ("Record")->add ($data);

if ($res 3) {echo "sent successfully";

} else {

echo "Send Failed";

} } }}

There is a small partner to note is that Ali big fish unauthenticated users, only to whitelist users to send text messages, so you have to configure a white list to ensure that we send the success! There is just when the application is free of 100 use of the number of bars for us to test the use!


Think dream php-Ali Big Fish 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.