From e1f3125b504ad12f0ee047c69e4185c14b6bed9e Mon Sep 17 00:00:00 2001 From: filesite Date: Sat, 27 Apr 2024 09:07:28 +0800 Subject: [PATCH] add frequence limit function --- controller/Controller.php | 17 +++++- .../tajian/controller/FrontapiController.php | 61 +++++++++++++++++++ views/layout/error.php | 4 +- www/js/tajian.js | 2 +- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index 6734f1a..478650c 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -162,8 +162,23 @@ Class Controller { } } + //error log + protected function logError($error_message) { + if (!empty(FSC::$app['config']['debug'])) { + $thisUrl = FSC::$app['requestUrl']; + $logTime = date('Y-m-d H:i:s'); + $logDir = __DIR__ . '/../runtime/logs/'; + $logFilename = 'error.log'; + $logOk = @error_log("{$logTime}\t{$thisUrl}\tERROR: {$error_message}\n", 3, "{$logDir}{$logFilename}"); + if (!$logOk) { //try to mkdir + @mkdir($logDir, 0700, true); + @error_log("{$logTime}\t{$thisUrl}\ttERROR: {$error_message} ms\n", 3, "{$logDir}{$logFilename}"); + } + } + } + //get user real ip - protected function getUserIp() { + protected function getUserIp() { $ip = false; if (!empty($_SERVER["HTTP_CLIENT_IP"])) { diff --git a/themes/tajian/controller/FrontapiController.php b/themes/tajian/controller/FrontapiController.php index cb44fc4..b3a30eb 100644 --- a/themes/tajian/controller/FrontapiController.php +++ b/themes/tajian/controller/FrontapiController.php @@ -372,4 +372,65 @@ eof; exit; } + //请求频率限制 + /** + * key: 检查频率限制的唯一标识 + * max: 最大次数 + * time: 检查时间,单位:秒 + */ + protected function requestLimit($key, $max, $time) { + $isLimited = false; + + try { + session_start(); + + $current_time = microtime(true)*1000; + + $field = md5("requestLimit_by_{$key}"); + $field_update_time = "{$field}_updated"; + if (!empty($_SESSION[$field]) && !empty($_SESSION[$field_update_time]) && $current_time - $_SESSION[$field_update_time] <= $time*1000) { + $_SESSION[$field] ++; + }else { + $_SESSION[$field] = 1; + $_SESSION[$field_update_time] = $current_time; + } + + if ($_SESSION[$field] > $max) { + $isLimited = true; + } + }catch(Exception $e) { + $this->logError("Request limit by session failed: " . $e->getMessage()); + } + + return $isLimited; + } + + //获取短信验证码 + public function actionSendsmscode() { + echo "Building..."; + exit; + } + + //新用户注册 + public function actionCreateuser() { + $ip = $this->getUserIp(); + $check_time = 120; //2 分钟内 + $max_time_in_minutes = 5; //最多 5 次 + + $isUserGotRequestLimit = $this->requestLimit($ip, $max_time_in_minutes, $check_time); + if ($isUserGotRequestLimit) { + $this->logError("Request limit got, ip: {$ip}"); + throw new Exception('Oops,操作太快了,请喝杯咖啡休息会吧...'); + } + + echo "Building..."; + exit; + } + + //用户登录 + public function actionLoginuser() { + echo "Building..."; + exit; + } + } diff --git a/views/layout/error.php b/views/layout/error.php index a62d59b..5122a1d 100644 --- a/views/layout/error.php +++ b/views/layout/error.php @@ -5,9 +5,9 @@ diff --git a/www/js/tajian.js b/www/js/tajian.js index 8469898..d66c6b5 100644 --- a/www/js/tajian.js +++ b/www/js/tajian.js @@ -203,7 +203,7 @@ if ($('.bt_kf_JS').get(0)) { var win_width = $(window).width(); if (win_width > 768 && $('.tajian_index').get(0)) { - $('.bt_kf_JS').click(); + $('.bt_kf_JS').click().addClass('hide'); } }