Home >Backend Development >PHP Problem >How to implement mobile phone registration in php

How to implement mobile phone registration in php

王林
王林Original
2021-10-14 16:59:482184browse

php method to implement mobile phone registration: 1. Put the interface address and appkey in the configuration file; 2. Encapsulate the sendmsg function and use curl to send the request; 3. Define the sendcode method in the controller; 4. Call The sendmsg function implements the verification code SMS sending function.

How to implement mobile phone registration in php

The operating environment of this article: windows10 system, php 7, thindpad t480 computer.

We usually need to send an SMS verification code when registering with a mobile phone number. We also need to send an SMS verification code through a mobile phone number when performing sensitive operations such as changing passwords. So what should you do if you want to send an SMS verification code in an actual project? Usually it is necessary to call the SMS sending interface of the third-party SMS provider.

Let’s take a look at how to achieve it!

Mobile phone registration:

You can put the interface address and appkey in the configuration file. Encapsulate a function sendmsg for sending text messages. You can use the curl request method in PHP (curl function library in PHP) to send the request.

if (!function_exists('sendmsg')) {
    function sendmsg($phone, $msg){
        //从配置文件读取接口信息
        $gateway = config('msg.gateway');
        $appkey = config('msg.appkey');
        //准备请求地址
        $url = $gateway . "?appkey=" . $appkey . "&mobile=" . $phone . "&content=" . $msg;
        //发送请求 比如get方式  https请求
        $res = curl_request($url, false, [], true);
 
        if (!$res) {
            return "请求发送失败";
        }
        //请求发送成功,返回值json格式字符串
        $arr = json_decode($res, true);
        if ($arr['code'] == 10000) {
            return true;
        }
        return $arr['msg'];
    }
}

Define a sendcode method in the controller. When the front desk clicks to send the verification code, it sends an ajax request. This method receives the mobile phone number of the registered user at the front desk and calls the sendmsg function to implement the verification code SMS sending function.

 //ajax请求发送注册验证码
    public function sendcode($phone)
    {
        //参数验证
        if (empty($phone)) {
            return ['code' => 10002, 'msg' => '参数错误'];
        }
        //短信内容  您用于注册的验证码为:****,如非本人操作,请忽略。
        $code = mt_rand(1000, 9999);
        $msg = "您用于注册的验证码为:{$code},如非本人操作,请忽略。";
        //发送短信
        $res = sendmsg($phone, $msg);
        if ($res === true) {
            //发送成功,存储验证码到session 用于后续验证码的校验
            session('register_code_' . $phone, $code);
            return ['code' => 10000, 'msg' => '发送成功', 'data' => $code];
        }
        return ['code' => 10001, 'msg' => $res];
    }

Email registration:

Email registration in PHP can use the PHPMailer plug-in to send emails (see the PHPMailer manual for details). Configure the email account information in the configuration file, encapsulate a send_email function and use phpmailer to send emails.

if (!function_exists('send_email')) {
    //使用PHPMailer发送邮件
    function send_email($email, $subject, $body){
        //实例化PHPMailer类  不传参数(如果传true,表示发生错误时抛异常)
        $mail = new PHPMailer();
//        $mail->SMTPDebug = 2;                              //调试时,开启过程中的输出 
        $mail->isSMTP();                                     // 设置使用SMTP服务
        $mail->Host = config('email.host');                  // 设置邮件服务器的地址
        $mail->SMTPAuth = true;                              // 开启SMTP认证
        $mail->Username = config('email.email');             // 设置邮箱账号
        $mail->Password = config('email.password');            // 设置密码(授权码)
        $mail->SMTPSecure = 'tls';                            //设置加密方式 tls   ssl
        $mail->Port = 25;                                    // 邮件发送端口
        $mail->CharSet = 'utf-8';                           //设置字符编码
        //Recipients
        $mail->setFrom(config('email.email'));//发件人
        $mail->addAddress($email);     // 收件人
        //Content
        $mail->isHTML(true);                                  // 设置邮件内容为html格式
        $mail->Subject = $subject; //主题
        $mail->Body    = $body;//邮件正文
//      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        if ($mail->send()) {
            return true;
        }
        return $mail->ErrorInfo;
//        $mail->ErrorInfo
    }
}

Then call this function in the controller method to implement the function of sending verification emails to registered user mailboxes.

Recommended learning: php training

The above is the detailed content of How to implement mobile phone registration in php. 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