PHP code example of hotzhan / think-verifycode

1. Go to this page and download the library: Download hotzhan/think-verifycode library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hotzhan / think-verifycode example snippets


        use hotzhan\verifycode\sms\SmsVerifyCode;
        
        //注册验证码
        public function regSms(SmsVerifyCode $sms)
        {
            //前端获取到手机号码
            $param = request()->param();
            $phoneNumber = $param['mobile'];
            //发送注册验证码
            $sms->sendRegisterSms($phoneNumber);
            //返回验证码发送结果,结果里包含token,需要返回给前端,验证时前端需要提交这个token
            return $sms->getResultData();
        }
        
        //验证码校验
        public function checkSms(SmsVerifyCode $sms)
        {
            $param = request()->param();
            //$param里的字段根据自己前端的设置
            $phoneNumber = $param['mobile'];
            $code = $param['code'];
            $token = $param['smstoken'];
            //校验验证码和对应的手机号
            $res = $sms->checkSmsVerifyCode($token, $code, $phoneNumber);
            if(!$res)//验证码校验不通过
                return $sms->getResultData();
        }
        


        use hotzhan\verifycode\mail\MailVerifyCode;
        
        //注册验证码
        public function regMail(MailVerifyCode $mail)
        {
            //前端获取到手机号码
            $param = request()->param();
            $mailAddress = $param['address']; //[email protected]
            //给对应邮箱发送验证码
            $mail->sendVerifyCodeMail($mailAddress)
            //返回验证码发送结果,结果里包含token,需要返回给前端,验证时前端需要提交这个token
            return $mail->getResultData();
        }
        
        //验证码校验
        public function checkMail(MailVerifyCode $mail)
        {
            $param = request()->param();
            //$param里的字段根据自己前端的设置
            $mailAddress = $param['address'];
            $code = $param['code'];
            $token = $param['mailtoken'];
            //校验验证码和对应的邮箱
            $res = $mail->checkMailVerifyCode($token, $code, $mailAddress);
            if(!$res)//验证码校验不通过
                return $sms->getResultData();
        }