<?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;
}
}
}
缓存(或者session)生成短信验证码
最新推荐文章于 2023-06-29 08:54:03 发布
本文介绍了一个短信验证码管理系统的设计与实现,包括验证码的生成、存储及验证流程。系统采用PHP语言开发,利用缓存技术提高效率,并详细说明了核心方法如生成验证码、设置缓存及验证缓存中的验证码等功能。

1793

被折叠的 条评论
为什么被折叠?



