短信验证码的失效时间

怎么给短信验证码设置失效时间呢?除了session,请各位大神给点建议

阅读 23.9k
35 个回答

1.生成验证码
2.让验证码结合时间的概念,比如

  • 存到缓存(redis、memcache等,以手机号为key,验证码为value),设置半小时过期,最后用户提交的时候,去看下缓存还在不在,对不对
  • 存到数据库,表结构为phone,code,expire_time,生成数据存到数据库,最后用户提交去数据库查
  • 如果不是短信验证码,是邮箱验证,还可以用邮箱、过期时间等信息加密,用户点击确认的时候解密,确认时间是否过期
  • 想不粗来了

主要通过时间来控制, 用redis, memcache, mysql, session什么的都只是个实现工具, 具体还是要看业务的需求.

memcached可以设置过期时间

Redis::set()
1、数据库存发短信的相关信息 手机号 验证码 入库时间 等等 根据入库时间去判断是否过期 或者直接入库一个过期时间
2、缓存 缓存应该是比较方便的 发送时直接存个过期时间 验证的时候取出来一对比 不用查库 至于用什么缓存redis还是什么都可以 根据自身情况而定

我司是使用redis::set()

这里使用的是TP5框架 代码参考一下

/**
 * 发送验证码
 */
public function sendSmsCode() {
    // 获取手机号
    $phone = input ('phone',0);
    // DO:判断手机号是否注册 调用验证器
    $count = \app\common\model\Users::where(['phone'=>$phone])->count ();
    if ($count > 0 ) return json (['code' => 200, 'msg' => '手机号已存在', 'data' => json([])]);

    // 判断是否有验证码 处在 60秒内有效期
    if (Cache::has ('reg_sms_code:'.$phone)) {
        $old_cache_data = cache ('reg_sms_code:'.$phone);
        $last_time = $old_cache_data['send_time'];
        if ($last_time + 60 > time ()) {
            return json (['code' => 200, 'msg' => '请等60秒后,再次发送验证码', 'data' => json([])]);
        }
    }
    // 正则匹配长度
    $preg_str = "/^1\d{10}$/";
    $str = preg_match($preg_str, $phone);
    //判断是否正则通过
    if ($str == 0) {
        return json(['code' => 200, 'msg' => '手机号码格式错误 :' . $phone, 'data' => json([])]);
    }

    // 将验证码记录入缓存
    $code = mt_rand (100000,999999);
    $expire_time = time () + 300;
    $cache_data = [
        'phone' => $phone,
        'code' => $code,
        'expire_time' =>$expire_time, #过期时间
        'send_time' => time () #发送时间
    ];

    // DO:发送验证码
    Loader::import('alisms\Alisms', EXTEND_PATH);
    AliSms::sendSms($phone,$code);
    // 将验证码信息加入缓存
    cache ('reg_sms_code:'.$phone,$cache_data,300);

    return json (['code' => 200, 'msg' => '验证码发送成功','data' => json([])]);


}

在生成验证码的时候不是会往数据库插一条记录吗,同时会记录时间,在验证验证码的时候判断当前手机,验证码的这条记录的时间是否过期就可以了,使用 mysql ,redis 都可以

新手上路,请多包涵

An RPL Report needs to be submitted by IT professionals who want to migrate to Australia but do not have a degree or diploma which is considered equivalent to an Australian degree or diploma in the related areas by the authority.

新手上路,请多包涵
新手上路,请多包涵

Recognition of Prior Learning (RPL) is nothing but the assessment of skills of the candidates who are willing to migrate to Australia. The candidates from other countries should have necessary skills and knowledge before they can migrate to Australia. Australian Computer Society (ACS) is an association that assesses the candidates based on their skills and knowledge. It is for the Information and Communications Technology (ICT) professionals and has around 22,000 members all over Australia. The professionals from other countries, with no ICT qualifications or insufficient ICT, have to showcase their skills and knowledge to the ACS for Australia Skilled Migration. This is the reason the candidates without any ICT qualifications are required to submit rpl australia report form for skill assessment.

新手上路,请多包涵

Students aiming for a career in programming and computer language are often increasingly attracted to the opportunities offered by the reputed universities located in the United Kingdom. But studying in such distinguished institutions always presents with some hurdles, i.e., complex and elaborate assignments. For many out there, preparing these complex assignments requires endless struggles, and it almost seems like waging war. But with our computer programming help services, all your assignment woes would be a thing of the past.

新手上路,请多包涵

Confused about how to do an assignment? For any law assignment help, get in touch with the best assignment writing service in Australia. Here, you will find an optimum-quality solution for all your assignment needs. Enroll now!

新手上路,请多包涵
新手上路,请多包涵
新手上路,请多包涵

GotoAssignmentHelp.com is leading plagiarism free assignment writing help in Australia. We provide customized assignment help service. We are leading the market for more than a decade now and have acquired the name of being the best academic help service for our comprehensive services at pocket-friendly rates.
online conflict management assignment help
write project management assignment
supply chain management assignment help
project management assignment helponline
strategy management assignment help online
assignment on porter's five forces model
history assignment help online
sociology assignment help</a

新手上路,请多包涵
新手上路,请多包涵
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏