Home >PHP Framework >ThinkPHP >ThinkPHP6 SMS verification code integration: realizing mobile phone verification function

ThinkPHP6 SMS verification code integration: realizing mobile phone verification function

WBOY
WBOYOriginal
2023-08-25 16:55:491555browse

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

  1. Make sure you have installed the ThinkPHP6 framework, or you can install it through composer
  2. Register an account with an SMS service provider, such as Alibaba Cloud, Tencent Cloud, Cloud Pian, etc., and purchase relevant SMS service packages

2. Integrate the SMS verification code function

  1. Install the SMS sending extension package
    Execute the following command in the root directory of the ThinkPHP6 project to install the SMS sending extension package:
    composer require overtrue/easy-sms -vvv
  2. Configure SMS service
    In the config directory in the root directory of the project, Create a new sms.php file and add the following configuration:
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',  // 你的短信签名名称
        ],
    ],
];
  1. Create a verification code sending class
    In the common directory under the app directory, create a new Sms class for sending Verification code:
<?php
namespace appcommon;

use thinkacadeCache;
use thinkacadeConfig;

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;
    }
}
  1. Call the verification code sending class
    Where you need to send the verification code, call the sendCode method of the Sms class to send the verification code:
<?php
namespace appindexcontroller;

use appcommonSms;

class User
{
    public function sendSmsCode()
    {
        $phoneNumber = '手机号码';
        Sms::sendCode($phoneNumber);
    }
}
  1. Verification verification code
    Where you need to verify the verification code entered by the user, you can verify it in the following ways:
<?php
namespace appindexcontroller;

use thinkacadeCache;

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!

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