PHP SMS Verification Code implementation process detailed PHP example

Source: Internet
Author: User
This article mainly for you in detail the PHP mobile phone text Verification code implementation process, with a certain reference value, interested in small partners can refer to

I am in my own blog (Laravel) registered part of the use of mobile phone number registration, need to send SMS verification code.

The use of cloud-chip SMS service providers, of course, specific SMS service providers are free to choose.

1. Implementation process

Enter your phone number and click Get Verification Code
After submitting the correct SMS verification code, the registration is completed.

2, realize the idea map

3, registered cloud tablets, as well as the development of information authentication, template settings , here is not detailed unfolded

4, installation easy-sms,easy-sms is Ang Sul write a text message sending component, using this component, we can quickly realize the SMS send function.


Composer require "overtrue/easy-sms"//New Profile Touch config/easysms.php


Then add the following inside the easysms.php file:


<?php  return [    ' timeout ' =>5.0,    ' default ' =>[      //Gateway Invoke policy, defaults: Sequential call      ' strategy ' + \ Overtrue\easysms\strategies\orderstrategy::class,      //default available send Gateway      ' gateways ' = [        ' Yunpian ',      ],    ],    //Available gateway configuration    ' gateways ' = ['      errorlog ' = ' = '        file ' = '/tmp/easy-sms.log ',      ] ,      ' Yunpian ' = [        ' api_key ' + env (' Yunpian_api_key '),],]    ,;


Then create a serviceprovider


PHP Artisan Make:provider Easysmsserviceprovider


Modify a file

app/providers/easysmsserviceprovider.php


<?phpnamespace App\providers;use Illuminate\support\serviceprovider;use Overtrue\easysms\easysms;class Easysmsserviceprovider extends serviceprovider{  /**   * Bootstrap services.   *   * @return void   *  /Public Function boot ()  {    //  }  /**   * Register services.   *   * @return void   *  /Public Function register ()  {    $this->app->singleton (easysms:: Class,function ($app) {      return new easysms (config (' easysms '));    });    $this->app->alias (easysms::class, ' easysms ');}  }


Finally open the config/app.php in providers to add App\providers\easysmsserviceprovider::class,

5, get the api_key of cloud slice

To configure Yunpian_api_key in. env, note that you need to replace it with your own KEY

6, the controller code to obtain the Verification Code (Code and key into the cache)



Public Function Getverificationcode ($request) {if (FALSE = = = $this->validateapirequest ($request->all (), [ ' Mobile ' = ' required|regex:/^1[34578]\d{9}$/|unique:users '],[' mobile.required ' = ' Please enter your mobile number ', ' Mobil    E.regex ' + ' phone number format is incorrect ', ' mobile.unique ' = ' phone number already exists ']) {return false;    } $mobile = Trim ($request->get (' mobile '));    $code = Str_pad (Random_int (1,9999), 4,0,str_pad_left); try{$easySms->send ($mobile, [' content ' = ' and ' Uknow ' Your verification code is {$code}.    If I do not operate, please ignore this text message "]);      }catch (\guzzlehttp\exception\clientexception $exception) {$response = $exception->getresponse ();      $result =json_decode ($response->getbody ()->getcontents (), true); $this->setmsg ($result [' msg ']??      ' SMS sending exception ');    return false;    } $key = ' Verificationcode '. Str_random (15);    $expiredAt = Now ()->addminutes (1);    Cache::p ut ($key, [' mobile ' = $mobile, ' code ' = ' $code], $expiredAt); return [' VerificatiOn_key ' + $key, ' expiredat ' + $expiredAt->todatetimestring (), ' verification_code ' + $code];} 


7. Comparison Verification Code


Public Function Userstore ($mobile, $verification _key, $code, $password, $password _confirmation) {$params = [' Mobile ' =&G t; $mobile, ' verification_key ' = $verification _key, ' code ' = $code, ' password ' and ' $password, ' password_confirm  ation ' = $password _confirmation]; The parameter determines if (FALSE = = = $this->validateapirequest ($params, [' mobile ' = ' required|regex:/^1[34578]\d{9}$/|uniqu E:users ', ' code ' = ' Required ', ' verification_key ' = ' required ', ' Password ' and ' required|min:6|confirmed ' ', ' password_confirmation ' = ' required ',], [' mobile.required ' = ' Please enter phone number ', ' mobile.regex ' + ' phone number format Incorrect ', ' mobile.unique ' = ' + ' mobile number already exists ', ' code.required ' = ' Please enter the SMS Verification Code ', ' password.required ' = ' Please enter the password ', ' PA Ssword.min ' + ' password must not be less than 6 bits ', ' password.confirmed ' + ' password inconsistent ', ' password_confirmation.required ' = ' Please re-enter the password '  , ' verification_key.required ' = ' Please enter SMS Verification Code ']) {return false; } $verifyData = Cache::get ($veriFication_key);   if (! $verifyData) {$this->setmsg (' Verification Code expired ');  return false;   } if (!hash_equals ($code, (String) $verifyData [' Code ']) {$this->setmsg (' captcha error ');  return false;  } cache::forget ($verification _key);  $user = User::create ([' mobile ' = $mobile, ' password ' =>bcrypt ($password)]);   if (! $user) {$this->setmsg (' registration failed ');  return false; } return true;


The above process is the basic steps of 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.