最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP手機(jī)短信驗證碼實現(xiàn)流程詳解

 更新時間:2018年05月17日 09:27:38   作者:范特西Fantasy  
這篇文章主要為大家詳細(xì)介紹了PHP手機(jī)短信驗證碼的實現(xiàn)流程,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本人在自己博客(Laravel)的注冊部分 使用手機(jī)號注冊,需要發(fā)送短信驗證碼。

使用云片的短信服務(wù)提供商,當(dāng)然具體短信服務(wù)提供商大家可以自由選擇。

1、實現(xiàn)流程

輸入手機(jī)號,點擊獲取驗證碼
提交正確的短信驗證碼后,注冊完成

2、實現(xiàn)思路圖

3、注冊 云片,以及開發(fā)信息認(rèn)證,模板設(shè)置,這里就不詳細(xì)展開了

4、安裝 easy-sms,easy-sms 是安正超寫的一個短信發(fā)送組件,利用這個組件,我們可以快速的實現(xiàn)短信發(fā)送功能。

composer require "overtrue/easy-sms"
//新建配置文件
touch config/easysms.php

然后在 easysms.php 文件內(nèi) 添加以下內(nèi)容:

 <?php

  return [

    'timeout'=>5.0,
    'default'=>[
      // 網(wǎng)關(guān)調(diào)用策略,默認(rèn):順序調(diào)用
      'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

      // 默認(rèn)可用的發(fā)送網(wǎng)關(guān)
      'gateways' => [
        'yunpian',
      ],
    ],
    // 可用的網(wǎng)關(guān)配置
    'gateways' => [
      'errorlog' => [
        'file' => '/tmp/easy-sms.log',
      ],
      'yunpian' => [
        'api_key' => env('YUNPIAN_API_KEY'),
      ],
    ],

];

然后創(chuàng)建一個 ServiceProvider

php artisan make:provider EasySmsServiceProvider

修改文件

app/providers/EasySmsServiceProvider.php

<?php

namespace 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');
  }
}

最后 打開config/app.php 在 providers 中增加 App\Providers\EasySmsServiceProvider::class,

5、獲取云片的API_KEY

在.env中配置 YUNPIAN_API_KEY,注意下面需要替換為你自己的 key

6、控制器代碼 獲取驗證碼(將code 以及key存入緩存)

public function getVerificationCode($request)
  {
    if(FALSE === $this->validateApiRequest($request->all(),
        ['mobile' => 'required|regex:/^1[34578]\d{9}$/|unique:users'],[
          'mobile.required'=>'請輸入手機(jī)號',
          'mobile.regex'=>'手機(jī)號格式不正確',
          'mobile.unique'=>'手機(jī)號已存在'
        ])){
      return false;
    }

    $mobile = trim($request->get('mobile'));
    $code = str_pad(random_int(1,9999),4,0,STR_PAD_LEFT);


    try{
       $easySms->send($mobile,
        ['content'=>"【UKNOW】您的驗證碼是{$code}。如非本人操作,請忽略本短信"]       );

    }catch(\GuzzleHttp\Exception\ClientException $exception){

      $response = $exception->getResponse();
      $result =json_decode($response->getBody()->getContents(),true);
      $this->setMsg($result['msg']?? '短信發(fā)送異常');
      return false;
    }

    $key = 'verificationCode'.str_random(15);
    $expiredAt = now()->addMinutes(1);
    Cache::put($key,['mobile'=>$mobile,'code'=>$code],$expiredAt);

    return [
      'verification_key'=>$key,
      'expiredAt'=>$expiredAt->toDateTimeString(),
      'verification_code'=>$code
      ];
}

7、對比驗證碼

public function userStore($mobile, $verification_key,$code,$password,$password_confirmation)
 {

  $params = [
   'mobile'=>$mobile,
   'verification_key'=>$verification_key,
   'code'=>$code,
   'password'=>$password,
   'password_confirmation'=>$password_confirmation
  ];
  //參數(shù)判斷
  if (
   FALSE === $this->validateApiRequest($params, [
    'mobile' => 'required|regex:/^1[34578]\d{9}$/|unique:users',
    'code' => 'required',
    'verification_key'=>'required',
    'password'  => 'required|min:6|confirmed',
    'password_confirmation' => 'required',
   ], [
    'mobile.required' => '請輸入手機(jī)號',
    'mobile.regex' => '手機(jī)號格式不正確',
    'mobile.unique' => '手機(jī)號已存在',
    'code.required' => '請輸入短信驗證碼',
    'password.required' => '請輸入密碼',
    'password.min'   => '密碼不得小于6位',
    'password.confirmed' => '密碼前后不一致',
    'password_confirmation.required'=>'請再次輸入密碼',
    'verification_key.required'=>'請輸入短信驗證碼'
   ])
  ) {
   return false;
  }

  $verifyData = Cache::get($verification_key);
  if( !$verifyData){
   $this->setMsg('驗證碼已失效');
   return false;
  }
  if(!hash_equals($code,(string)$verifyData['code'])){
   $this->setMsg('驗證碼錯誤');
   return false;
  }

  Cache::forget($verification_key);
  $user = User::create([
   'mobile'=>$mobile,
   'password'=>bcrypt($password)
  ]);
  if(!$user){
   $this->setMsg('注冊失敗');
   return false;
  }
  return true;
}

以上流程就是手機(jī)驗證碼基本步驟。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宜黄县| 嘉黎县| 漳浦县| 青川县| 莲花县| 兴安县| 兴安县| 西乌珠穆沁旗| 石家庄市| 锦州市| 上思县| 久治县| 南通市| 石棉县| 康马县| 依安县| 军事| 昔阳县| 饶平县| 棋牌| 新竹县| 博兴县| 仪陇县| 南华县| 大方县| 老河口市| 四会市| 巩义市| 崇州市| 垫江县| 丹阳市| 萍乡市| 中卫市| 沂源县| 修武县| 河曲县| 江西省| 桑日县| 开远市| 兴城市| 新兴县|