2020-07-29 22:05:09后端控制表单重复提交
您现在的位置是: 首页 > php > 后端控制表单重复提交
基于tp5,通过redis写的防止表单重复提交
use think\Cache;
class Common {
/**
* desc 防止表单重复提交
* @param null $key
* @param int $expire_time
* @return bool
* 2020/7/29
*/
public static function setConcurrentLimit($key = null, $expire_time = 10) {
// 程序关闭时执行该函数
static $registerShutdownFunction = false;
if ($registerShutdownFunction === false) {
$registerShutdownFunction = true;
register_shutdown_function(array('self', 'setConcurrentLimit'));
}
// 并发性重复提交限制逻辑
static $cacheKey = array();
// 如果缓存key为null,则清除缓存key
if ($key === null) {
foreach ($cacheKey as $k) {
Cache::rm($k);
}
return true;
}
// 设置缓存key
$key = md5('boke_setConcurrentLimit_' . $key);
$cacheKey[] = $key;
$current_time = time();
// 给定的key不存在时,返回true
if (cache::init()->handler()->setnx($key, $current_time)) {
cache::init()->headler()->setTimeout($key, $expire_time);
return true;
} else {
$cache_time = cache::get($key);
if ($cache_time && ($current_time - $cache_time >= $expire_time)) {
cache::rm($key);
return true;
}
}
return false;
}
}
关键字词: 后端控制表单多次提交
下一篇: 创建文件