這篇文章主要介紹“php手機驗證碼實現(xiàn)的方法”,在日常操作中,相信很多人在php手機驗證碼實現(xiàn)的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php手機驗證碼實現(xiàn)的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
10年積累的網(wǎng)站建設(shè)、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先做網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有丹東免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
php手機驗證碼的實現(xiàn)方法:首先注冊云片以及開發(fā)信息認(rèn)證,并進行模板設(shè)置;然后在“easysms.php”文件內(nèi)添加“'default'=>[]”等內(nèi)容;接著獲取云片的API_KEY;最后通過控制器代碼獲取驗證碼即可。
本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1、Dell G3電腦。
PHP手機短信驗證碼實現(xiàn)流程詳解
本人在自己博客(Laravel)的注冊部分 使用手機號注冊,需要發(fā)送短信驗證碼。
使用云片的短信服務(wù)提供商,當(dāng)然具體短信服務(wù)提供商大家可以自由選擇。
1、實現(xiàn)流程
輸入手機號,點擊獲取驗證碼
提交正確的短信驗證碼后,注冊完成
2、實現(xiàn)思路圖
3、注冊 云片,以及開發(fā)信息認(rèn)證,模板設(shè)置,這里就不詳細展開了【】
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'=>'請輸入手機號', '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']?? '短信發(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' => '請輸入手機號', '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; }
以上流程就是手機驗證碼基本步驟。
到此,關(guān)于“php手機驗證碼實現(xiàn)的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站題目:php手機驗證碼實現(xiàn)的方法
網(wǎng)頁鏈接:http://www.2m8n56k.cn/article48/giodhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站內(nèi)鏈、手機網(wǎng)站建設(shè)、網(wǎng)站維護、自適應(yīng)網(wǎng)站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)