缓存(或者session)生成短信验证码

<?php
namespace app\common\smslxx;

use think\Cache;

/**
 *  
 *
 * Class Smslxx
 * 使用缓存,缓存键前缀sms
 * @package app\common\smslxx
 */
class Smslxx
{
    const CODE_LENTH  = 4; //验证码长度,最大9
    const CLOSE_TIME  = 3; //验证码过期时间
    const CHECK_CLOSE = true; //验证成功销毁
    public function get_sms_code($phone = '')
    {
        if (empty($phone)) {
            return -1;
        }
        $code = $this->made_sms_code(self::CODE_LENTH);
        Cache::set("sms{$phone}", $code, self::CLOSE_TIME);
        return $code;
    }
    public function made_sms_code($codelenth = self::CODE_LENTH)
    {
        return rand(pow(10, ($codelenth - 1)), pow(10, $codelenth) - 1);
    }
    public function check_sms_code($phone = '', $code = '')
    {
        if (empty($phone)) {
            return -1;
        }
        if (!Cache::has("sms{$phone}")) {
            return -1;
        }
        $cachecode = Cache::get("sms{$phone}");
        if ($code == $cachecode) {
            if (self::CHECK_CLOSE) {
                Cache::rm("sms{$phone}");
            }
            return 1;
        } else {
            return -1;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值