Home>Article>Backend Development> Detailed explanation of PHP mobile phone SMS verification code implementation process

Detailed explanation of PHP mobile phone SMS verification code implementation process

不言
不言 Original
2018-06-01 11:34:22 4079browse

This article mainly introduces the implementation process of PHP mobile phone SMS verification code in detail. It has certain reference value. Interested friends can refer to

I wrote on my blog (Laravel) In the registration part, you need to use your mobile phone number to register, and you need to send an SMS verification code.

SMS service providers who use Yunpian, of course, you can freely choose the specific SMS service provider.

1. Implementation process

Enter your mobile phone number and click to get the verification code
After submitting the correct SMS verification code, the registration is completed

2. Implementation idea map

3. Registration of cloud films, development information authentication, and template settingswill not be detailed here.

4. Install easy-sms, easy-smsis a text message sending component written by Anzhengchao. Using this component, we can quickly implement the text message sending function.

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

Then add the following content in the easysms.php file:

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'), ], ], ];

Then Create a ServiceProvider

php artisan make:provider EasySmsServiceProvider

Modify the file

app/providers/EasySmsServiceProvider.php

app->singleton(EasySms::class,function ($app){ return new EasySms(config('easysms')); }); $this->app->alias(EasySms::class,'easysms'); } }

Finally open config/app.php and add App\Providers\EasySmsServiceProvider::class in providers,

5. Get the API_KEY of the cloud slice

Configure in .env YUNPIAN_API_KEY, note that the following needs to be replaced with your own key

6, controller code to obtain the verification code (store the code and key in the cache)


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. Compare the verification 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; }

The above process is the basic steps of the mobile phone verification code.

Related recommendations:

PHP implements login verification code verification function

##

The above is the detailed content of Detailed explanation of PHP mobile phone SMS verification code implementation process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn