Home >PHP Framework >ThinkPHP >ThinkPHP6 SMS verification code integration: realizing mobile phone verification function
ThinkPHP6 SMS verification code integration: realizing mobile phone verification function
Foreword:
In modern society, mobile phones have become one of the indispensable tools in people’s lives. one. In website or APP development, verification of user mobile phone numbers is also a common functional requirement. This article will introduce how to integrate the SMS verification code function in the ThinkPHP6 framework to realize the mobile phone verification function.
1. Preparation
2. Integrate the SMS verification code function
return [ 'default' => [ 'gateways' => [ 'aliyun', // 阿里云短信服务 ], ], 'gateways' => [ 'aliyun' => [ 'access_key_id' => 'your-access_key_id', // 你的阿里云access_key_id 'access_key_secret' => 'your-access_key_secret', // 你的阿里云access_key_secret 'sign_name' => 'your-sign_name', // 你的短信签名名称 ], ], ];
<?php namespace appcommon; use thinkacadeCache; use thinkacadeConfig; class Sms { // 发送验证码 public static function sendCode($phoneNumber) { // 生成随机验证码 $code = mt_rand(100000, 999999); // 发送短信 $result = EasySmsFacadesEasySms::send($phoneNumber, [ 'template' => 'your-template-id', // 你在短信服务提供商处创建的短信模板ID 'data' => [ 'code' => $code, ], ]); // 验证码存入缓存,有效时间为5分钟 Cache::set('sms_code:' . $phoneNumber, $code, 300); return $result; } }
<?php namespace appindexcontroller; use appcommonSms; class User { public function sendSmsCode() { $phoneNumber = '手机号码'; Sms::sendCode($phoneNumber); } }
<?php namespace appindexcontroller; use thinkacadeCache; class User { public function checkCode() { $phoneNumber = '手机号码'; $code = '用户输入的验证码'; // 从缓存中获取正确的验证码 $correctCode = Cache::get('sms_code:' . $phoneNumber); // 验证用户输入的验证码是否正确 if ($code == $correctCode) { // 验证通过 // 进行相关操作 } else { // 验证失败 // 提示用户验证码错误 } } }
3. Summary
Passed In the above steps, we successfully integrated the SMS verification code function in the ThinkPHP6 framework and implemented the mobile phone verification function. When the user needs to conduct mobile phone verification, he or she can send a verification code so that the user can enter the correct verification code for verification. This can increase the security of operations such as user login, registration, and modification of important information.
Finally, it should be noted that when purchasing SMS service packages, choose according to your actual needs to avoid wasting resources and costs. In addition, in order to prevent malicious text messages from being sent, there are generally certain restrictions, such as only a certain number of text messages can be sent per minute, only a certain number of text messages can be sent per day, etc. In actual use, pay attention to using the SMS verification code function according to the regulations and configuration of the SMS service provider.
The above is the detailed content of ThinkPHP6 SMS verification code integration: realizing mobile phone verification function. For more information, please follow other related articles on the PHP Chinese website!