這篇文章帶給大家的內容是關於 laravel框架下php手機簡訊驗證碼實現流程,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
具體簡訊服務提供者大家可以自由選擇。輸入手機號,點選取得驗證碼
提交正確的簡訊驗證碼後,註冊完成
composer require "overtrue/easy-sms" //新建配置文件 touch config/easysms.php
然後在easysms.php 檔案內加入以下內容:
<?php return [ 'timeout'=>5.0, 'default'=>[ // 网关调用策略,默认:顺序调用 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class, // 默认可用的发送网关 'gateways' => [ 'yunpian', ], ], // 可用的网关配置 'gateways' => [ 'errorlog' => [ 'file' => '/tmp/easy-sms.log', ], 'yunpian' => [ 'api_key' => env('YUNPIAN_API_KEY'), ], ], ];
php artisan make:provider EasySmsServiceProvider
#
<?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'); } }
App\Providers\EasySmsServiceProvider::class,
public function getVerificationCode($request) { if(FALSE === $this->validateApiRequest($request->all(), ['mobile' => 'required|regex:/^1[34578]\d{9}$/|unique:users'],[ 'mobile.required'=>'请输入手机号', 'mobile.regex'=>'手机号格式不正确', 'mobile.unique'=>'手机号已存在' ])){ 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']?? '短信发送异常'); 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 ]; //参数判断 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' => '请输入手机号', 'mobile.regex' => '手机号格式不正确', 'mobile.unique' => '手机号已存在', '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; }
# 相關推薦:
thinkphp範本如何判斷是手機微信付款還是微信掃碼支付PHP想要實現頁面跳轉功能具體怎麼操作? (函數標籤範例)以上是laravel框架下php手機簡訊驗證碼實作流程的詳細內容。更多資訊請關注PHP中文網其他相關文章!