当前位置:网站首页>TP5 restrict access frequency
TP5 restrict access frequency
2022-06-27 18:23:00 【When you're lonely, the dog stays with you】
effect

1. Turn on Redis

Open your Redis Software If not, it can be in Small skin Environmental Science Installed in software management

2 .tp5 To configure config.php

'cache' => [
// Driving mode
'type' => 'redis',
// Cache used by default
'default' => [
// Driving mode
'type' => 'redis',
// Cache save Directory
'path' => CACHE_PATH,
],
// Cache prefix
'prefix' => '',
// Cache lifetime 0 Indicates permanent cache
'expire' => 0,
// redis cache
'redis' => [
// Driving mode
'type' => 'redis',
// Server address
'host' => '127.0.0.1',
],
],3.php Code
function requestAccess($time=1,$limit=3){
// Get access to the user's IP
$ip=md5(request()->ip());
// Get the access interface path
$path=request()->path();
// take IP And the access interface path md5 Encrypted into a string , This represents the interface accessed by the same customer .
$UV=md5($ip.$path);
// Every IP And the number of times the interface cannot exceed per minute
$cacheIp=Cache::get($UV)?:0;
if($cacheIp){
if($cacheIp>$limit){
return false;
}else{
Cache::inc($UV,1);
}
}else{
Cache::set($UV,1,60);
}
// For each request IP Address 、 Parameters and paths are spliced into an identical interface of the same user .
$post=json_encode(request()->post());
$name=md5($path.$post);
// How long can each same data not be requested
$cache=Cache::get($name);
if($cache==$ip){
return false;
}else{
Cache::set($name,$ip,$time);
return true;
}
}4. call
When you need to limit the frequency of interface calls
if (!$this->requestAccess()){
echo json(['code'=>200,'msg'=>' Interface calls are too frequent '])->send();die;
}边栏推荐
- Dark horse programmer - software testing foundation class -02-30-45 tools open browser running code, audio, video, test points, audio and video labels, layout labels. Advanced hyperlink syntax, absolu
- Introduction to photoswape
- Repeatedly recognized by authoritative organizations! Smartbi was selected into the top three lists of high-tech and high growth enterprises
- Shardingsphere & Atlas & MYCAT comparison
- 印象深刻的问题
- (5) SPI application design and simulation verification 2 - design code implementation
- [fxcg] today's market analysis
- About redis master-slave replication
- JS event binding and common events
- SQLite net (SQLite is used by unity, WPF and WinForm)
猜你喜欢

Lvgl8.x migrating to stm32f4

建立自己的网站(10)

【多线程】线程通信调度、等待集 wait() 、notify()

Oracle概念二

Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'

The power of code refactoring: how to measure the success of refactoring

Why should string be designed to be immutable?

Electronic smart package tutorial

Sword finger offer 22 The penultimate node in the linked list
![[multithreading] thread communication scheduling, waiting set wait(), notify()](/img/4b/b9312fafd1ef1bf438f7aec7665276.png)
[multithreading] thread communication scheduling, waiting set wait(), notify()
随机推荐
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Computing trends in three times of machine learning
Unity 阴影——阴影平坠(Shadow pancaking)
印象深刻的问题
Explain the distributed computing of Apache skywalking OAP in detail
Part 31 supplement (31) ECMAScript conversion to string and number
Leetcode 5. Longest Palindromic Substring
Wanzhou gold industry: what knowledge points do you need to master to invest in precious metals?
Study on heritability and field experiment design
1. introduction to MariaDB
seata-server 端数据库连接用户、业务数据库 undo_log 所属用户都需要什么权限?
(POJ - 1847) tram (shortest circuit)
Halcon: discrete digital OCR recognition
电子智慧套装教程
Oracle概念二
医院预约挂号系统-系统结构
Wanzhou gold industry: what are the differences between gold t+d investment and other investments?
软件测试基础-软件测试历史流程,分类,好处,限制