PHP中手机注册、邮箱注册的功能实现

      通常在使用手机号注册时需要发送短信验证码,在修改密码等敏感操作时也需要验证手机号发送短信验证码。在项目代码中发送短信,通常要调用第三方短信商的短信发送接口。

      可以将接口地址和appkey放在配置文件中。封装一个函数sendmsg用于发送短信,可以用PHP中的curl请求方式(PHP中的curl函数库)发送请求。

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'];
    }
}
在控制器里定义一个sendcode方法,当前台点击发送验证码发送ajax请求,该方法接收到前台注册用户的手机号,调用sendmsg函数实现验证码短信发送功能。
 //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];
    }
PHP中邮箱注册可以使用PHPMailer插件来实现邮件发送(具体可查看PHPMailer手册)。在配置文件中配置好邮箱账号信息,封装一个send_email函数使用phpmailer发送邮件。
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
    }
}
然后在控制器的方法中调用该函数,实现本邮箱向注册用户邮箱发送验证邮件功能。



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值