PHP code example of lx9912 / think-throttle

1. Go to this page and download the library: Download lx9912/think-throttle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lx9912 / think-throttle example snippets



return [
    \think\middleware\Throttle::class,
];


// 中间件配置
return [
    // 缓存的键,true 表示使用来源ip
    'key' => true,
    // 设置访问频率,此处指的是允许每分钟请求10次。默认值 null 表示不限制
    'visit_rate' => '10/m',
    // 访问受限时返回的http状态码
    'visit_fail_code' => 429,
    // 访问受限时访问的文本信息
    'visit_fail_text' => '访问频率受到限制,请稍等__WAIT__秒再试', 
    // 返回信息格式 1 返回json格式 0 返回html格式
    'Return_type' => 0 
];

'key' => function($throttle, $request) {
    $user_id = $request->session->get('user_id');
    return $user_id ;
},

'key' => function($throttle, $request) {
    return '__CONTROLLER__/__ACTION__/__IP__';
},

'key' => '__CONTROLLER__/__ACTION__/__IP__',

'key' => function($throttle, $request) {
    $throttle->setRate('5/m');                      // 设置频率
    $throttle->setDriverClass(CounterSlider::class);// 设置限流策略
    return true;
},