当前位置:Gxlcms > PHP教程 > php手机验证码怎么实现

php手机验证码怎么实现

时间:2021-07-01 10:21:17 帮助过:10人阅读

php手机验证码的实现方法:首先注册云片以及开发信息认证,并进行模板设置;然后在“easysms.php”文件内添加“'default'=>[]”等内容;接着获取云片的API_KEY;最后通过控制器代码获取验证码即可。

本文操作环境:Windows7系统、PHP7.1、Dell G3电脑。

PHP手机短信验证码实现流程详解

本人在自己博客(Laravel)的注册部分 使用手机号注册,需要发送短信验证码。

使用云片的短信服务提供商,当然具体短信服务提供商大家可以自由选择。

1、实现流程

输入手机号,点击获取验证码
提交正确的短信验证码后,注册完成

2、实现思路图

3、注册 云片,以及开发信息认证,模板设置,这里就不详细展开了【推荐:《PHP视频教程》】

4、安装 easy-sms,easy-sms 是安正超写的一个短信发送组件,利用这个组件,我们可以快速的实现短信发送功能。

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

然后在 easysms.php 文件内 添加以下内容:

  1. <?php
  2. return [
  3. 'timeout'=>5.0,
  4. 'default'=>[
  5. // 网关调用策略,默认:顺序调用
  6. 'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
  7. // 默认可用的发送网关
  8. 'gateways' => [
  9. 'yunpian',
  10. ],
  11. ],
  12. // 可用的网关配置
  13. 'gateways' => [
  14. 'errorlog' => [
  15. 'file' => '/tmp/easy-sms.log',
  16. ],
  17. 'yunpian' => [
  18. 'api_key' => env('YUNPIAN_API_KEY'),
  19. ],
  20. ],
  21. ];

然后创建一个 ServiceProvider

  1. php artisan make:provider EasySmsServiceProvider

修改文件

app/providers/EasySmsServiceProvider.php

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Overtrue\EasySms\EasySms;
  5. class EasySmsServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Bootstrap services.
  9. *
  10. * @return void
  11. */
  12. public function boot()
  13. {
  14. //
  15. }
  16. /**
  17. * Register services.
  18. *
  19. * @return void
  20. */
  21. public function register()
  22. {
  23. $this->app->singleton(EasySms::class,function ($app){
  24. return new EasySms(config('easysms'));
  25. });
  26. $this->app->alias(EasySms::class,'easysms');
  27. }
  28. }

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

5、获取云片的API_KEY

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

6、控制器代码 获取验证码(将code 以及key存入缓存)

  1. public function getVerificationCode($request)
  2. {
  3. if(FALSE === $this->validateApiRequest($request->all(),
  4. ['mobile' => 'required|regex:/^1[34578]\d{9}$/|unique:users'],[
  5. 'mobile.required'=>'请输入手机号',
  6. 'mobile.regex'=>'手机号格式不正确',
  7. 'mobile.unique'=>'手机号已存在'
  8. ])){
  9. return false;
  10. }
  11. $mobile = trim($request->get('mobile'));
  12. $code = str_pad(random_int(1,9999),4,0,STR_PAD_LEFT);
  13. try{
  14. $easySms->send($mobile,
  15. ['content'=>"【UKNOW】您的验证码是{$code}。如非本人操作,请忽略本短信"] );
  16. }catch(\GuzzleHttp\Exception\ClientException $exception){
  17. $response = $exception->getResponse();
  18. $result =json_decode($response->getBody()->getContents(),true);
  19. $this->setMsg($result['msg']?? '短信发送异常');
  20. return false;
  21. }
  22. $key = 'verificationCode'.str_random(15);
  23. $expiredAt = now()->addMinutes(1);
  24. Cache::put($key,['mobile'=>$mobile,'code'=>$code],$expiredAt);
  25. return [
  26. 'verification_key'=>$key,
  27. 'expiredAt'=>$expiredAt->toDateTimeString(),
  28. 'verification_code'=>$code
  29. ];
  30. }

7、对比验证码

  1. public function userStore($mobile, $verification_key,$code,$password,$password_confirmation)
  2. {
  3. $params = [
  4. 'mobile'=>$mobile,
  5. 'verification_key'=>$verification_key,
  6. 'code'=>$code,
  7. 'password'=>$password,
  8. 'password_confirmation'=>$password_confirmation
  9. ];
  10. //参数判断
  11. if (
  12. FALSE === $this->validateApiRequest($params, [
  13. 'mobile' => 'required|regex:/^1[34578]\d{9}$/|unique:users',
  14. 'code' => 'required',
  15. 'verification_key'=>'required',
  16. 'password' => 'required|min:6|confirmed',
  17. 'password_confirmation' => 'required',
  18. ], [
  19. 'mobile.required' => '请输入手机号',
  20. 'mobile.regex' => '手机号格式不正确',
  21. 'mobile.unique' => '手机号已存在',
  22. 'code.required' => '请输入短信验证码',
  23. 'password.required' => '请输入密码',
  24. 'password.min' => '密码不得小于6位',
  25. 'password.confirmed' => '密码前后不一致',
  26. 'password_confirmation.required'=>'请再次输入密码',
  27. 'verification_key.required'=>'请输入短信验证码'
  28. ])
  29. ) {
  30. return false;
  31. }
  32. $verifyData = Cache::get($verification_key);
  33. if( !$verifyData){
  34. $this->setMsg('验证码已失效');
  35. return false;
  36. }
  37. if(!hash_equals($code,(string)$verifyData['code'])){
  38. $this->setMsg('验证码错误');
  39. return false;
  40. }
  41. Cache::forget($verification_key);
  42. $user = User::create([
  43. 'mobile'=>$mobile,
  44. 'password'=>bcrypt($password)
  45. ]);
  46. if(!$user){
  47. $this->setMsg('注册失败');
  48. return false;
  49. }
  50. return true;
  51. }

以上流程就是手机验证码基本步骤。

以上就是php手机验证码怎么实现的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行