輸入手機號,點擊獲取驗證碼
提交正確的短信驗證碼后,注冊完成
composer require overtrue/easy-sms //新建配置文件 touch config/easysms.php
然后在 easysms.php 文件內 添加以下內容:
?php return [ timeout = 5.0, default = [ // 網關調用策略,默認:順序調用 strategy = /Overtrue/EasySms/Strategies/OrderStrategy::html' target='_blank'>class, // 默認可用的發送網關 gateways = [ yunpian , // 可用的網關配置 gateways = [ errorlog = [ file = /tmp/easy-sms.log , yunpian = [ api_key = env( YUNPIAN_API_KEY ),
];
然后創建一個 ServiceProvider
php artisan make:provider EasySmsServiceProvider
修改文件
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 }
最后 打開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 = 請輸入手機號 , mobile.regex = 手機號格式不正確 , mobile.unique = 手機號已存在 ])){ return false; $mobile = trim($request- get( mobile $code = str_pad(random_int(1,9999),4,0,STR_PAD_LEFT);7,對比驗證碼
$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 }
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教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答