php 获取短信验证码

原理:我们自己生成随机数验证码,存入缓存中,并给手机发送验证码,登录时,获取输入的验证码与缓存中的验证码比对是否一致。
 


    /**
     * 获取验证码
     */
    public function captcha()
    {
        (new TelValidate())->goCheck('captcha');//验证规则
        $code = (new Captcha(input('param.tel')))->sendCaptcha();
        $this->result(1, '验证码已发送' . $code);
    }
         * 下发验证码
         * @return int 验证码
         */
    public function sendCaptcha()
    {
        $this->checkCodeNum();
        $code = rand(111111, 999999);//生成验证码
        $value_info = SystemSetModel::getValueByName('sms_body', 'config_value');
        $body = str_replace(
            '{generate_code}',
            $code,
            $value_info->config_value
        );
        
            $res = appSendSMS($this->mobile, $body);
            $res = json_decode($res, true);
            if (!isset($res['State']) && $res['State'] != 0) {
                self::json(0,'验证码发送失败');
            }
        $this->mobileCode($code);
        $this->setCodeNum();
        return $code;
    }


    /**
     * 存储/验证 验证码
     * @param int $code 验证码
     * @param int $type 1、存储,2、验证
     * @return bool
     */
    public function mobileCode($code, $type = 1)
    {
        if ($type == 1) {
            Cache::set($this->mobile, $code, 900);
            return true;
        } else {
            $logCode = Cache::get($this->mobile);
            if (!$logCode) {
                self::json(0,'验证码错误');
            }
            if ($code == $logCode) {
                Cache::rm($this->mobile);
                return true;
            } else {
                self::json(0,'验证码错误');
            }
        }
    }
   /**
     * 更新获取验证码次数与时间
     */
    private function setCodeNum() {
        //计算现在到明天凌晨的时间
        $tomorrow = strtotime(date('Y-m-d', strtotime('+1 day')));
        $ttl = $tomorrow - time();
        $logMobile = Cache::get('logMobile');
        $logMobile[$this->mobile]['num'] -= 1;
        $logMobile[$this->mobile]['time'] = time();
        Cache::set('logMobile', $logMobile, $ttl);
    }

公共文件:

if (!function_exists('appSendSMS')) {
    function appSendSMS($mobile, $body)
    {
        $url = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
        $send_data = "sname=dljudian&spwd=abc1234567&scorpid=&sprdid=1012888&sdst=" . $mobile . "&smsg=" . $body;
        $xmls = curl_post_raw($url, $send_data);
        $xml = simplexml_load_string($xmls);
        $json_result = json_encode($xml);
        return $json_result;
    } // end func
}

//模拟post请求,数据格式为raw
if (!function_exists('curl_post_raw')) {
    function curl_post_raw($url, $rawData)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'X-AjaxPro-Method:ShowList',
            'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
        ));
        $data = curl_exec($ch);
        curl_close($ch);
        return ($data);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值