使用PHP怎么实现一个手机短信验证码功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、实现流程
输入手机号,点击获取验证码
提交正确的短信验证码后,注册完成
2、实现思路图
3、注册 云片,以及开发信息认证,模板设置,这里就不详细展开了
4、安装 easy-sms,easy-sms 是安正超写的一个短信发送组件,利用这个组件,我们可以快速的实现短信发送功能。
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'),
],
],
];
然后创建一个 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']?? '短信发送异常');
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;
}
1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流的数据库。4、面向对象编程:PHP提供了类和对象。5、版本更新速度快。6、具有丰富的功能。7、可伸缩性。8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。